Strange behaviour of switch case with boolean value

前端 未结 5 1695
-上瘾入骨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:22

    The error you get is about string variable and not boolean possible values.

    The fact that there is no way that noone of cases run, is a true (in this case), but compiler doesn't go so far in analyzing the code. It just looks on variable that is not assigned and used in some conditions and there is not default one, so suppose that there could be some case when it remains unassigned.

    0 讨论(0)
  • 2020-12-19 19:26

    I think, you are trying to ask, why str variable is unassigned, as the switch statement's cases will assign it some value, but the compiler can't determine whether it will fall in any of the case statement, That is why you are getting this error on returning str.

    If you add a default case with string assignment, then the compiler will know for sure that, the str will hold some value and that is why you don't get error

    0 讨论(0)
  • 2020-12-19 19:32

    Writing a switch statement on a boolean variable seems kinda wasty to me. Why not use the conditional operator (?:):

    private string NumberToString(int number, bool flag)
    {
        return flag ? number.ToString("00") : number.ToString("0000"); 
    }
    

    The code seems a bit more concise and you don't need local variables.

    But back to your question about why your code doesn't compile => it is because variables must always be assigned and this assignment should not happen inside conditional statements.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-19 19:44

    Well, there's been good explanation for causes of this issue, but there is one more solution to the problem, which was not mentioned. Just put default instead of second case:

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

    It leaves compiler with no further thoughts about flag variable value as we always assign a default value in our switch statement.

    Also consider if-else statement equivalent solution:

        private string NumberToString(int number, bool flag)
        {
            string str;
    
            if (flag)
                str = number.ToString("00");
            else
                str = number.ToString("0000");
    
            return str;
        }
    

    I am not particularly fond of 'one-liners' (one line solutions), as they lack extendability and moreover - readability, but as the last resort just use ternary operators like this:

        private string NumberToString(int number, bool flag)
        {
            return flag ? number.ToString("00") : number.ToString("0000");
        }
    

    While I am at it - consider using extension method + defaulting boolean to false, for example:

    public static class intExtensions
    {
        public static string NumberToString(this int number, bool flag = false)
        {
            return flag ? number.ToString("00") : number.ToString("0000");
        }
    }
    

    and then in main class:

            int intVal = 56;
            string strVal1 = intVal.NumberToString(true);
            string strVal2 = intVal.NumberToString();
    
    0 讨论(0)
提交回复
热议问题