Oct. 25th, 2005
switch statements in C#
Oct. 25th, 2005 09:06 amDon't read any further unless you either know or care about (a) C# and (b) syntax
And no, this isn't a complaint about break - I'm very happy to make things explicit.
However, why is the format
switch (myInt)
{
case 1:
//Do Something
break;
case 2:
//Do Something Else
break;
default:
//Do nothing at all
break;
}
rather than something like:
switch(myInt)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}
That way the 'break' is subsumed into the closing curly brackets, and it follows the same way of grouping commands as the rest of the language does.
_Ideally_ it would go even further, to something even more like if statements:
switch(myInt)
{
case (1)
{
//Do Something
}
case (2)
{
//Do Something Else
}
default
// Do Nothing at all
}
and you wouldn't be able to use goto to jump from one case to another (you'd have to take any shared code and put it in a separate method).
This could then be extended into a more generalised form (less optimisable, but still very readable) for those occasions when you don't want to necessarily check a single variable, but instead want to check a variety of cases, each one of which is exclusive - like so:
switch()
{
case (Country == "US")
{
//Deal with Americans
}
case (Age < 18)
{
//Deal with foreign children
}
case (Age < 30)
{
//Deal with foreign young adults
}
default
{
//Deal with everyone else
}
}
This is much more readable than lots of if...else if...else if....else statements. And the structure doesn't break any existing code, so it would be possible to add without causing rewrites. It's also more like the rest of C#, in that it uses curly braces to hold blocks of code.
And no, this isn't a complaint about break - I'm very happy to make things explicit.
However, why is the format
switch (myInt)
{
case 1:
//Do Something
break;
case 2:
//Do Something Else
break;
default:
//Do nothing at all
break;
}
rather than something like:
switch(myInt)
{
case 1:
{
//Do Something
}
case 2:
{
//Do Something Else
}
default
// Do Nothing at all
}
That way the 'break' is subsumed into the closing curly brackets, and it follows the same way of grouping commands as the rest of the language does.
_Ideally_ it would go even further, to something even more like if statements:
switch(myInt)
{
case (1)
{
//Do Something
}
case (2)
{
//Do Something Else
}
default
// Do Nothing at all
}
and you wouldn't be able to use goto to jump from one case to another (you'd have to take any shared code and put it in a separate method).
This could then be extended into a more generalised form (less optimisable, but still very readable) for those occasions when you don't want to necessarily check a single variable, but instead want to check a variety of cases, each one of which is exclusive - like so:
switch()
{
case (Country == "US")
{
//Deal with Americans
}
case (Age < 18)
{
//Deal with foreign children
}
case (Age < 30)
{
//Deal with foreign young adults
}
default
{
//Deal with everyone else
}
}
This is much more readable than lots of if...else if...else if....else statements. And the structure doesn't break any existing code, so it would be possible to add without causing rewrites. It's also more like the rest of C#, in that it uses curly braces to hold blocks of code.