Re: Falling through on switch-cases
- From: "Peter Webb" <webbfamily@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 20 Mar 2008 15:26:16 +1100
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message news:MPG.224b45d96c3561ebb8e@xxxxxxxxxxxxxxxxxxxxxxx
Ben Voigt [C++ MVP] <rbv@xxxxxxxxxxxxx> wrote:Jon Skeet [C# MVP] wrote:
> Rudy Velthuis <newsgroups@xxxxxxxxxxxx> wrote:
>> Jon Skeet [C# MVP] wrote:
>>
>>> Ranges are a possibility, so long as you can always check that a
>>> value falls in only one range.
>>
>> The compiler should simply generate a compile error if the ranges
>> overlap, like it does in Pascal. Then, only one range can apply. If
>> you'd allow overlapping ranges, the first match could win.
>
> Yes - as I say, it's easy enough to tell for the built-in numeric
> types. There may be other types where it's not so obvious though.
switch is only allowed on built in integral types and string anyway
Well yes, at the moment - but if we're considering possible changes, we
should consider the possibility of a more flexible switch.
It really goes back to what the point of switch is. If it's to use a
lookup to avoid a series of if/else statements for performance reasons,
I'm not sure its place is really deserved in C#.
If its purpose is to find one matching piece of code from a number of
options, there are ways it could be significantly more flexible.
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
As a newbie/amateur, I was disappointed with both case statements and enums in C#; both are weaker than what I expected. (Almost everything else was way better than I expected!).
As I understand it, I have to write
..
..
..
thisday = (int) dayofweek.Sunday
..
..
..
switch (thisday);
{case (int) dayofweek.Sunday: dostuff();break;
case (int) dayofweek.Monday: dostuff(); break;
..
..}
What I was hoping for was:
thisday=Sunday;
switch (thisday);
{case Sunday: dostuff(); break;
case Monday: dostuff(); break;}
Or even:
switch(thisday);
{case Sunday: dostuff(); break;
case Monday..Friday: dostuff(); break;
case Saturday: dostuff(); break;}
The continual need to cast back to integers reduces the readability of the code.
Also, as someone else pointed out, the ++ operator for enums should loop. If you have a statement:
thisday++;
and thisday was (int)dayofweek.Saturday, it returns what in reality is an out-of-range enum (as there is no day of the week after Saturday).
What I think would have been more useful is thisday++ being equivalent to
thisday++; if (thisday>(int)dayofweek.Saturday) thisday=(int) dayofweek.Sunday;
or even better
thisday++; if (thisday>Saturday) thisday=Sunday;
Its probably too late to change this and keep backwards compatibility, so I think C# is stuck with ++ working for enums as it does for integers.
As somebody else pointed out, Pascal (now 30 years old) had user defined types which work a little like enums, but in a more readable, maintainable and intuitive form.
.
- Follow-Ups:
- Re: Falling through on switch-cases
- From: Jon Skeet [C# MVP]
- Re: Falling through on switch-cases
- References:
- Falling through on switch-cases
- From: K Viltersten
- Re: Falling through on switch-cases
- From: Jon Skeet [C# MVP]
- Re: Falling through on switch-cases
- From: Rudy Velthuis
- Re: Falling through on switch-cases
- From: Jon Skeet [C# MVP]
- Re: Falling through on switch-cases
- From: Rudy Velthuis
- Re: Falling through on switch-cases
- From: Jon Skeet [C# MVP]
- Re: Falling through on switch-cases
- From: Ben Voigt [C++ MVP]
- Re: Falling through on switch-cases
- From: Jon Skeet [C# MVP]
- Falling through on switch-cases
- Prev by Date: Multiple StateImageList
- Next by Date: Re: How to configure a client/server in development
- Previous by thread: Re: Falling through on switch-cases
- Next by thread: Re: Falling through on switch-cases
- Index(es):
Relevant Pages
|