Tuesday, October 6, 2009

Case Statement Superiority

C# allows you to switch on enums and strings in addition to the types supported by Java (char, int)!

Unlike Java, you have to explicitly specify fall-through if you have any logic in your statement.

Sample Java:
switch(myInt)
{
case 1:
callSomething();
case 2:
//this will still be executed for case one
callSomethingElse();
}

Equivalent C# Code:
switch(myInt)
{
case 1:
callSomething();
goto case 2;
case 2:
callSomethingElse();
}

No comments:

Post a Comment