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

前端 未结 5 1931
天涯浪人
天涯浪人 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:08

    Enums are not limited to values they represent. You can assign this:

    MyEnum v = (MyEnum)1000;
    

    And there would be no problem at all. Add a default to your switch and you'll handle all possible situations.

提交回复
热议问题