简体   繁体   English

C#方法并非所有代码路径都返回值

[英]C# Method not all code paths return a value

I'm creating the following method that returns a normalized decimal value. 我正在创建以下方法,该方法返回标准化的十进制值。 The AngleUnits is an enum that contains Degrees, Gradians, Radians, and Turns. AngleUnits是一个枚举,其中包含度,Gradians,Radians和Turns。 However after implementing this code I get "Angle.Normalize(...) not all code paths return a value". 但是,实现此代码后,我得到“ Angle.Normalize(...),并非所有代码路径都返回值”。 Not sure what I'm missing here as I am returning the decimal value. 当我返回十进制值时,不确定在这里缺少什么。 Thanks in advance. 提前致谢。

private static decimal Normalize(decimal value, AngleUnits units)
    {
        decimal normalizedValue;

        switch (units)
        {
            case AngleUnits.Degrees:
                if (value >= 0 && value <= 360)
                {
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value < 0)
                {
                    value = value + 360;
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value > 360)
                {
                    value = value - 360;
                    normalizedValue = value;
                    return normalizedValue;
                }
                break;

            case AngleUnits.Gradians:
                if (value >= 0 && value <= 400)
                {
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value < 0)
                {
                    value = value + 400;
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value > 400)
                {
                    value = value - 400;
                    normalizedValue = value;
                    return normalizedValue;
                }
                break;

            case AngleUnits.Radians:
                if (value >= 0 && value <= twoPi)
                {
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value < 0)
                {
                    value = value + twoPi;
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value > twoPi)
                {
                    value = value - twoPi;
                    normalizedValue = value;
                    return normalizedValue;
                }
                break;

            case AngleUnits.Turns:
                if (value >= 0 && value <= 1)
                {
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value < 0)
                {
                    value = value + 1;
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value > 1)
                {
                    value = value - 1;
                    normalizedValue = value;
                    return normalizedValue;
                }
                break;
        }
    }

You have got this error because you can have a call to this method with an angleunits that has no match in the switch statement and your method should return a method So the compiler is detecting this. 之所以会出现此错误,是因为您可以使用在switch语句中不匹配的angleunits调用此方法,并且您的方法应返回一个方法,因此编译器正在检测到此错误。

when you close your switch statement 当您关闭switch语句时

you should return a default value 您应该return a default value

or throw an exception 或抛出异常

Throw new Exception("no units was found  ")

here a snippet of your method 这是您的方法的摘要

  private static decimal Normalize(decimal value, AngleUnits units)
        {
            decimal normalizedValue;

            switch (units)
            {
                case AngleUnits.Degrees:
                    if (value >= 0 && value <= 360)
                    {
                        normalizedValue = value;
                        return normalizedValue;
                    }
                    else if (value < 0)
                    {
                        value = value + 360;
                        normalizedValue = value;
                        return normalizedValue;
                    }
                    else if (value > 360)
                    {
                        value = value - 360;
                        normalizedValue = value;
                        return normalizedValue;
                    }
                    break;

                default: throw new Exception("no Angleunits match was found");  






            }
            return value; 


        }

Your not handling the default case in the switch. 您未处理交换机中的default情况。

In the case when I am switch on an enum I like to throw an InvalidEnumArgumentException in the default . 在我打开enum的情况下,我喜欢在default抛出InvalidEnumArgumentException This ensures that if I add to the enum in the future and forget to update the switch statement I will fail fast. 这样可以确保,如果将来我添加到enum ,而忘记更新switch语句,我将很快失败。

The compiler is concerned about what will happen if the variable you are switching on does not match against any case that you provided. 编译器担心如果您打开的变量与您提供的任何情况都不匹配会发生什么。 If that happens, no return statement will be hit. 如果发生这种情况,将不会命中任何return语句。 Try this to fix it: 尝试解决此问题:

private static decimal Normalize(decimal value, AngleUnits units)
    {
        decimal normalizedValue;

        switch (units)
        {
            case AngleUnits.Degrees:
                if (value >= 0 && value <= 360)
                {
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value < 0)
                {
                    value = value + 360;
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value > 360)
                {
                    value = value - 360;
                    normalizedValue = value;
                    return normalizedValue;
                }
                break;

            case AngleUnits.Gradians:
                if (value >= 0 && value <= 400)
                {
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value < 0)
                {
                    value = value + 400;
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value > 400)
                {
                    value = value - 400;
                    normalizedValue = value;
                    return normalizedValue;
                }
                break;

            case AngleUnits.Radians:
                if (value >= 0 && value <= twoPi)
                {
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value < 0)
                {
                    value = value + twoPi;
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value > twoPi)
                {
                    value = value - twoPi;
                    normalizedValue = value;
                    return normalizedValue;
                }
                break;

            case AngleUnits.Turns:
                if (value >= 0 && value <= 1)
                {
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value < 0)
                {
                    value = value + 1;
                    normalizedValue = value;
                    return normalizedValue;
                }
                else if (value > 1)
                {
                    value = value - 1;
                    normalizedValue = value;
                    return normalizedValue;
                }
                break;
        }
        return null;
     }

You're missing the "default" value for the switch. 您缺少该开关的“默认”值。 You are using an enumeration and that seem to limit the switch cases to only the values you have created for the enumeration, but that's not true, an enumeration is really stored in a numeric type, by default int but can be set as byte short or long . 您正在使用的枚举和似乎限制开关的情况下只已为枚举创造的价值,但事实并非如此,枚举真的是存储在数字类型,默认为int但可以设置为byte shortlong Then any number can be casted to an enum, even if it's outside the range of values you have defined for it, that's why you must handle the "default" value. 然后,任何数字都可以转换为枚举,即使它超出了您为其定义的值的范围,这也就是为什么必须处理“默认”值的原因。

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

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