Friday, December 4, 2009

Switch Statement in C++ and C#

We all know that in C++, the following switch statement is not conventional, but perfectly fine.

char keystroke = getch();
switch( keystroke ) {
case 'a':
case 'b':
case 'c':
case 'd':
KeyABCDPressed();
break;
case 'e':
KeyEPressed();
break;
default:
UnknownKeyPressed();
break;
}


But in C#, this is not OK. "C# does not support an implicit fall through from one case label to another. The one exception is if a case statement has no code." Therefore, C# does not support implicit "goto" with "switch". But why?

No comments:

Post a Comment