简体   繁体   English

如何在C#中消除错误``并非所有代码路径都返回值''

[英]How to remove the error 'not all code paths return a value' in c#

I often get this error in C#: 'not all code paths return a value'. 我经常在C#中收到此错误:“并非所有代码路径都返回值”。 I know the main reason behind this is an if not followed by an else . 我知道这背后的主要原因是一个“ if没有”的else But what if I do not want the compiler to do anything if the condition inside if is not satisfied? 但是,如果我不想要的编译器做任何事情,如果里面的条件if不满意? For example, in the following program of trying to find the quotient without the use of / operator: 例如,在下面的尝试不使用/运算符的商的程序中:

namespace ConsoleApplication1
{
    class Program
    {
        public int Quotient( int dividend, int divisor)
        {
            int i, quotient = 0, remainder;
            i = divisor;
            while (i <= dividend)
            {
                i += divisor;
                quotient++;
                remainder = dividend - i;
                if (i == dividend || ((i < dividend) && (remainder < divisor)))
                {
                    return quotient;
                }
            }
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            Console.WriteLine("Enter the first number:");
            int a = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the second number:");
            int b = Convert.ToInt32(Console.ReadLine());
            Program obj = new Program();
            int quotient = obj.Quotient(a, b);
            Console.WriteLine("The quotient is " + quotient);
        }
    }
}

Here I want the compiler to return quotient if the condition (i == dividend || ((i < dividend) && (remainder < divisor))) is true. 在这里,如果条件(i == dividend || ((i < dividend) && (remainder < divisor)))为真,我希望编译器返回quotient In case this condition is false, I want the flow to go back to while (i <= dividend) and increase i and quotient . 如果此条件为假,我希望流程返回while (i <= dividend)并增加iquotient But the compiler says 'not all code paths return a value'. 但是编译器说“并非所有代码路径都返回值”。 How am I supposed to get rid of this error? 我应该如何摆脱这个错误? What am I supposed to give as the block of else ? 我应该给else呢? Also, is there a try-catch method to overcome this error? 另外,是否有一种try-catch方法来克服此错误?

The return type of Quotient is int but if the condition in while loop is not met int wont be return. 商的返回类型为int但如果不满足while循环中的条件,则不会返回int Compiler ensures that function must return the int value. 编译器确保函数必须返回int值。 You only have return statement under condition and compiler assumes that condition might not met and method wont return any value and gives you error . 您仅在条件下具有return语句,并且编译器假定条件可能不满足,并且方法不会返回任何值并给您错误 Return some value un-conditionally from method. 从方法无条件返回一些值。

public int Quotient( int dividend, int divisor)
{
    int i, quotient = 0, remainder;
    i = divisor;
    while (i <= dividend)
    {
        i += divisor;
        quotient++;
        remainder = dividend - i;
        if (i == dividend || ((i < dividend) && (remainder < divisor)))
        {
            return quotient;
        }
    }
    return 0;
}

The reason you get that error is because if your code does not go into the if (i == dividend || ((i < dividend) && (remainder < divisor))) condition or it fails the function has nothing to return - for simplicity you could just return 0 as a default: 出现该错误的原因是,如果您的代码没有进入if (i == dividend || ((i < dividend) && (remainder < divisor)))条件,或者该函数失败,则该函数将无法返回-对于简单起见,您可以默认返回0

public int Quotient( int dividend, int divisor)
{
    int i, quotient = 0, remainder;
    i = divisor;
    while (i <= dividend)
    {
        i += divisor;
        quotient++;
        remainder = dividend - i;
        if (i == dividend || ((i < dividend) && (remainder < divisor)))
        {
            return quotient;
        }
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM