3

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?

BendEg
  • 20,098
  • 17
  • 57
  • 131
  • falling from case to case is not permitted, unless you use goto statement, either ways, you'll have to abide by scope roles (just declare the variable outside of the switch statement) – Tawfik Khalifeh Jul 29 '15 at 10:33

1 Answers1

4

Because you're not starting a new scope. Personally, I almost exclusively use block scopes in my case statements:

switch (test)
{
    case "hello":
    {
        string demo = "123";
        break;
    }
    case "world":
    {
        var demo = "1234";
        break;
    }
    case "hello world":
    {
        var demo = 34;
        break;
    }
}

In my opinion, the main reasons for this are 1) simplicity, and 2) compatibility with C. There already is a syntax for starting a new block scope, and that's using { ... }. No need to add another rule "just because". In C#, there's not much point in not having a separate scope for each of the case statements, since you are prohibited from reading possibly unassigned variables.

For example, the following isn't allowed in C#:

switch (test)
{
  case 1: string demo = "Hello"; goto case 2;
  case 2: demo += " world"; break;
}

Of course, the solution to this is rather easy - just declare the local outside of the switch scope and give it a default value if needed.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • Yep, i know this. But why do i have to do it on my own? I don't see any good case where the default behaviour is needed. – BendEg Jul 29 '15 at 10:33
  • BendEg, A case does not define a scope since `case "a":case "b": something` is valid – Sayse Jul 29 '15 at 10:33
  • Yes, but a term could start at the begining of a case and could be closed with a break or return... But it makes sence, that there is only one symbol, in this case `{ and }` which opens and close scopes... – BendEg Jul 29 '15 at 10:35
  • 1
    @BendEg Simplicity and C-compatibility are probably the most important part. C# is quite easy to parse in general, and adding more rules "just because" doesn't sound like a good idea. Of course, since it's illegal to access a possibly uninitialized local in C#, there's not much point - if you need something common to all the cases, just declare the local outside of the `switch` scope (this can be useful in e.g. explicit fallthroughs (using `goto case "world";` for example). – Luaan Jul 29 '15 at 10:44
  • Thank you. Would you like to add this to your answer? Than i will accept it :) Thank you! – BendEg Jul 29 '15 at 10:47