Strange behaviour of switch case with boolean value
Solution 1
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.
Solution 2
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.
Solution 3
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
Solution 4
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
Nikhil Agrawal
Updated on March 05, 2020Comments
-
Nikhil Agrawal about 4 years
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 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; }
Error is
Use of unassigned local variable 'str'
. Bool can only take true or false. So it will populatestr
in either case. Then why this error?Moreover this error is gone if along with true and false case I add a
default
case, but still what can a bool hold apart from true and false?Why this strange behaviour with bool variable?
-
Nikhil Agrawal almost 12 yearsYou said assignment should not happen inside conditional statements. Right? Wrong. Add a default case conditional statement and it will not give error.
-
Nikhil Agrawal almost 12 yearsBut bool always holds a value as it is value type.
-
Darin Dimitrov almost 12 yearsYes, adding a default case statement would indeed fix the error. But as I said, writing a switch statement with true, false and default cases seems absolutely insane and a good candidate for thedailywtf.com :-)
-
Habib almost 12 yearsyes, but the C# compiler doesn't take that into account, I am trying to find an official source for that, will post it as soon as I find it
-
Tim Schmelter almost 12 years@NikhilAgrawal: It's to keep it as simple as possible for the compiler and also to prevent the programmer from simple bugs. So if you show that you've thought a little bit about it(default case), the compiler allows it.
-
Nikhil Agrawal almost 12 yearsWhat if there was more lines of code inside switch case? What you have provided is an alternative and not an explanation to cause.
-
Darin Dimitrov almost 12 yearsI guess that's because of your oversimplified example. If there are other lines, simply use
if/else
with boolean variables, not switches. With if/else you don't need to worry about assigning as well and you have one less thing to worry about => the default case which doesn't make any sense with boolean variables. -
Tim Schmelter almost 12 years@NikhilAgrawal: You can find the explanation at the end of Eric Lipperts answer. "The reason this is illegal in C# is because using an unassigned local has high likelihood of being a bug. We simply make it illegal, and then the compiler prevents you from ever having such a bug."
-
Nikhil Agrawal almost 12 yearsI think Tim Schmelter just posted it. stackoverflow.com/a/8933935/284240
-
nawfal over 10 yearsOr even better:
return number.ToString(flag ? "00" : "0000")
:)