I'm a little bit confused about the use of terms in switch-case conditions. Why does c# takes all in a case
declared variables an declares them automatically at the top of a switch
statement?
For example:
switch (test)
{
case "hello":
string demo = "123";
break;
case "world":
demo = "1234";
break;
// not working
case "hello world":
demo = demo + "1234567";
break;
}
I am able to assign the variable demo
under case "world"
, even if it's declared under case "hello
. But c# seems only to declare the value and don't set any value, because getting and settings the value, (see under case "hello world"
is not possible.
Why is c# not opening a term/scope for every case-block and close it with a simple break or return?