You can make the scopes non-overlapping using braces:
switch (something)
{
case 1: {
int Number;
}
break;
case 2: {
float Number;
}
break;
}
Going out of scope is the only way a variable name is “deleted” in the sense you are talking about. Notably and unlike some other languages, C# doesn’t allow hiding local variables with other variables in more limited scopes — the scopes have to be non-overlapping (and in C#, that means from the opening brace, not simply the point of declaration!). What I mean is that this code, which is legal in C and C++ (not sure about Java) will cause a compiler error in C#:
int Number;
{
float Number;
}
1
solved How can I use the same name for two or more different variables in a single C# function? [closed]