Strange behaviour of switch case with boolean value
Posted
by
Nikhil Agrawal
on Stack Overflow
See other posts from Stack Overflow
or by Nikhil Agrawal
Published on 2012-06-19T09:13:55Z
Indexed on
2012/06/19
9:15 UTC
Read the original article
Hit count: 340
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 populate str
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?
© Stack Overflow or respective owner