Combine return and switch

后端 未结 12 2064
长情又很酷
长情又很酷 2021-02-03 22:15

How can I combine return and switch case statements?

I want something like

return switch(a)
       {
          case 1:\"lalala         


        
12条回答
  •  独厮守ぢ
    2021-02-03 22:25

    We can have one use case where we may need to return the value from condition written inside the switch; let's say:

    public void SomeMethod(SomeType enumType)  
    {   
        switch (enumType)  
        {  
            case a:  
                if (condition)  
                {  
                    if (condition1  && condition2)  
                    {  
                        return true;  
                    }  
                }  
                return false;  
                //break; break is actually not be required here if return used before break  
            case b:  
                if (condition)  
                {  
                    if (condition3  && condition4)  
                    {  
                        return true;  
                    }  
                }  
                return false;  
                // break;  
            default:  
                return false;  
                //break;  
        }  
    
        Public enum SomeType  
        {  
            a,  
            b,  
            c,  
            d  
        }  
    

提交回复
热议问题