switch statement

M

Mark Broadbent

How on earth did Anders & co arrive at this awful switch/ case/ select
statement?
Not only does it lose the abilty to do an intelligent switch a-la VB
eg. using >, <, list of values etc
.... but it also lacks a "C# feel"
e.g. I would have expected a brace pair to be required between each case
label
.... and the break to prevent fall thru is pointless?

Am I misguided in thinking that Anders has tried to adhere to the C++ case
without thinking about improving this logical construct?
=OR=
Am I miguided about it's usefulness (or lack of) ... and I do know you can
jump to different labels?
=OR=
Has is been made like this for a design reason (i.e. to prevent certain
kinds of poor logical design)
=OR=
A.N.Other.

Be interested in any opinions.


--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 
D

Daniel O'Connell [C# MVP]

Mark Broadbent said:
How on earth did Anders & co arrive at this awful switch/ case/ select
statement?
Not only does it lose the abilty to do an intelligent switch a-la VB
eg. using >, <, list of values etc
... but it also lacks a "C# feel"

Personally, I think the ranged switching is very *VB* ish and would be a
bigger loss to C# feel than anything that currently exists. A switch using
complex expressions like that is better and clearer written as a series of
ifs, IMHO.
e.g. I would have expected a brace pair to be required between each case
label
... and the break to prevent fall thru is pointless?

Justin Rogers wrote a reply a little while ago that explains why the break
is there and why there isn't a need for braces:
http://groups.google.com/groups?q=g...m=eN#[email protected]&rnum=7
Am I misguided in thinking that Anders has tried to adhere to the C++ case
without thinking about improving this logical construct?

It could have stood some small improvement, although ranges aren't one of
them(but no more fallthrough was).
 
N

Nicholas Paldino [.NET/C# MVP]

A switch using complex expressions like that is better and clearer written
as a series of
ifs, IMHO.

Coincidentally, that is exactly how VB compiles it down, as a series of
if statements.
 
D

Daniel O'Connell [C# MVP]

Nicholas Paldino said:
Coincidentally, that is exactly how VB compiles it down, as a series of
if statements.

I suspected as much, but I didn't have time to do IL tests. I know both VB
and C# compile to if statements when you switch on strings, but I havn't had
time to test VB with integers.
 
M

Mark Broadbent

I agree, yes very VB-ish, but I did kinda like it. I guess as my coding
improves I'll finally consign the rest of that VB hockus-pocus to history.

Thanks for the link, I was impressed. I am now making you a Google.MVP :)


--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

I would like ranged switch, but the compiler is the reason against this, as
I understand it. I have not found many instances where the current system is
a huge inconvenience, however.

I agree with the lack of fallthrough, as more logical errors are created for
the minimal amout of convenience. But, this may just be me.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
M

mikeb

Daniel said:
I suspected as much, but I didn't have time to do IL tests. I know both VB
and C# compile to if statements when you switch on strings, but I havn't had
time to test VB with integers.

Actually the C# switch statement on strings is a bit smarter than doing
the equivalent set of 'if' statements. It checks that the string is
interned (which all of the case targets will be), then does simple
reference equality tests to determine the code to jump to.

A series of 'if' statements will call op_Equals() on each test.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top