goto case null

  • Thread starter Thread starter Clive Dixon
  • Start date Start date
C

Clive Dixon

If C# (VS 2003) allows switch on null, why does it not allow "goto case
null":

switch (someString)
{
case "BlahBlahBlah":
break;

case null:
break;

default:
goto case null;

}

Compiler error:
"No such label 'case 0:' within the scope of the goto statement"

Is this an omission or is there some good reason for disallowing it?
 
Dnia Thu, 2 Nov 2006 14:19:56 -0000, Clive Dixon napisa³(a):
Compiler error:
"No such label 'case 0:' within the scope of the goto statement"

case 0 is not a label :)
 
Agreed. But what I have written is "case null", not "case 0". I wouldn't
have thought it beyond the wit of the compiler writers to make this work.

It seems that I can work around this by writing "goto case (string)null".
Clearly the compiler is assuming for some reason that I'm doing a switch on
type 'int' even though it's on type 'string', and converting null to 0.
(Though I'm not clear why it would do this. This is C#, not C++...)
 
This is boviously a bug in the c#-compiler of VS2003. Yes, it does interpret
'null' as zero in the 'goto case'. I tested it with an int switch
expression.

As i tested also, this is corrected in VS2005.
 
Back
Top