andrewducker (
andrewducker) wrote2005-10-25 09:06 am
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
switch statements in C#
Don'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.
no subject
switch (foo) {
case a:
case b:
// do this
break;
case c:
// do something else
break;
default:
// do something different
}
no subject
Which is basically what I'm complaining about :->
no subject
And yes, C/C# should have better switch statements (or even pattern matching). I think COmega has some of this kind of thing (it's an extension of C# with some native-XML and functional programming goodies.) Also, the Cyclone project looked at adding some nicer things to C, including switch statements with pattern matching/arbitrary tests (and enough type and control flow analysis to warn you if you forgot a "break"!)
But given the 20-odd year delay for obviously good features moving from research prototypes to mainstream languages (and given that the next mainstream language isn't due for 5-10 years), you may just have to wait.
no subject
I'll take a look at COmega when I have a mo - cheers for the pointer.
no subject
Gives me hope that one of these days I'll pick up C after all..
no subject
So if you had them for defining the code for a given switch statement you couldn't do the following
case a:
//some code for just a
case b:
//some code for a and b
break;
case c:
//some code for just c
break;