Re: switch, case, and how it sometimes lets you fallthrough
- From: "Mike Schilling" <mscottschilling@xxxxxxxxxxx>
- Date: Sun, 11 Dec 2005 23:29:42 -0800
"Benny Raymond" <benny@xxxxxxxxxxxxxxx> wrote in message
news:%23DuDVGq$FHA.2036@xxxxxxxxxxxxxxxxxxxxxxx
> I'm confused as to how fallthrough is limited in switch. For example the
> following works:
>
> string switch_test = "a";
> switch (switch_test)
> {
> case "a":
> case "b":
> case "c":
> doSomething(a);
> break;
> }
>
> but the following does not work:
> string switch_test = "a";
> switch (switch_test)
> {
> case "a":
> if (<some test>)
> break;
> case "b":
> case "c":
> doSomething(a);
> }
You have to think of a C# switch differently than a C or C++ swtich. In C,
a switch looks like:
switch (value)
{
label1:
statement-list-1
label2:
statement-list-2
...
}
Any statment list can be empty, and you have fallthrough.
In C#, it's
switch(value)
{
label1A:
label1B:
label1C:
...
statment-list-1;
label2A:
label2B:
label2C:
...
statment-list-2;
}
A list of labels has to have at least one member. There is no fallthrough
from one statement list to the next.
.
- References:
- switch, case, and how it sometimes lets you fallthrough
- From: Benny Raymond
- switch, case, and how it sometimes lets you fallthrough
- Prev by Date: Re: Why is there a differnce between Print Preview and Print to printe
- Next by Date: Re: DataGridView in 2005
- Previous by thread: Re: switch, case, and how it sometimes lets you fallthrough
- Next by thread: Beginner interface question
- Index(es):
Relevant Pages
|