Strange behaviour of switch case with boolean value

前端 未结 5 1696
-上瘾入骨i
-上瘾入骨i 2020-12-19 19:19

My question is not about how to solve this error(I already solved it) but why is this error with boolean value.

My function is

private string NumberT         


        
5条回答
  •  粉色の甜心
    2020-12-19 19:39

    private string NumberToString(int number, bool flag)
    {
        string str = "";
    
        switch(flag)
        {
            case true: 
                str = number.ToString("00");
                break;
            case false:
                str = number.ToString("0000"); 
                break;
        }
    
        return str;
    }
    

    write this string str = ""; - you should assign value

    if you add default case there is no chance to fall through the switch cases without assigning. Now it is

提交回复
热议问题