为什么在使用switch语句和枚举时会出现"Not all code paths return a value"的错误提示?

19 浏览
0 Comments

为什么在使用switch语句和枚举时会出现"Not all code paths return a value"的错误提示?

我有以下代码:

public int Method(MyEnum myEnum)
{
    switch (myEnum)
    {
        case MyEnum.Value1: return 1;
        case MyEnum.Value2: return 2;
        case MyEnum.Value3: return 3;
    }
}
public enum MyEnum
{
    Value1,
    Value2,
    Value3
}

然后我得到错误提示:"Not all code paths return a value"。我不明白这个switch语句怎么可能不会跳转到指定的一个case中。

一个enum能否以某种方式为null

0