Why do “Not all code paths return a value” with a switch statement and an enum?

前端 未结 5 1939
天涯浪人
天涯浪人 2020-12-17 08:28

I have the following code:

public int Method(MyEnum myEnum)
{
    switch (myEnum)
    {
        case MyEnum.Value1: return 1;
        case MyEnum.Val         


        
5条回答
  •  误落风尘
    2020-12-17 09:16

    It has to be either:

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

    or:

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

提交回复
热议问题