tags:

views:

87

answers:

2

Possible Duplicate:
Case Statement Block Level Declaration Space in C#

For example:

string danger;
switch ( this.Type )
{
    case Warfare.Nuclear:
    case Warfare.Biological:
    case Warfare.Chemical:
        danger = "deadly";
        break;

    case Warfare.Air:
        string threat = "major"

        ...

        break;

    case Warfare.Ground:
        string threat = "medium"

        ...

        break;
}

Why can't I just define local variables with the same name in each, without having to use different names or declaring the variable outside the switch statement which won't be used outside it anyways.

+3  A: 

A case statement does not define a variable scope. You could add something in curly braces inside your case statement to define a new variable scope.

Bob
+1  A: 

Enclose the case blocks in {} to narrow the scope of the identically-named variables

Steve Townsend