hello.I'm new to c# and I want to know if there is any difference in "switch case" between c++ and c#?
hello.I'm new to c# and I want to know if there is any difference in "switch case" between c++ and c#?
The most important difference is that in C# you can "switch" on strings.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
look, somewhere in my program I have written the code below:
and later in my program I have written the following code:Code:enum Direction { North, South, East, West, Up, Down, Here };
it draws a red line under North, South,...giving this error:Code:switch (direction) { case North: return South; case South: return North; case East: return West; case West: return East; case Up: return Down; case Down: return Up; }
The name 'North' does not exist in the current context.
I have no clue what is wrong with that.
I read somewhere that in C# switch, there is no fall-through for the cases .if there is any difference in "switch case" between c++ and c#?
DISREGARD - Was wrong. <Thank: Magos>
Last edited by manasij7479; 07-06-2011 at 06:30 AM.
Use "Direction.South" inistead of just "South".
Yes there is fall-through, if by that you mean:
Code:case 1: case 2: case 3: break; default: break;
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
One important difference is that you cannot fall through in C#. You'll get compiler warnings if you try. You can goto other labels:
Code:switch(someNumber) { case 1: goto case 2; case 2: goto case 3; case 3: MessageBox.Show("You chose: " + someNumber); break; default: MessageBox.Show("You did not choose 1, 2, or 3."); break; }
If you understand what you're doing, you're not learning anything.
The following code works perfectly fine for me, no errors, no warnings (warning level 4, highest), tested in framework 2.0, 3.5 and 4.0:
Code:switch(someNumber) { case 1: case 2: case 3: MessageBox.Show("You chose: " + someNumber); break; default: MessageBox.Show("You did not choose 1, 2, or 3."); break; }
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
Sorry, you can fall through empty cases, but if you try something like this:
... you get (notice it doesn't complain about case 4) ...Code:static void Main(string[] args) { int num = 7; switch (num) { case 3: Console.WriteLine("Doing something specific for 3 and falling through..."); case 4: case 7: Console.WriteLine("The number was: " + num); break; default: Console.WriteLine("The number was not 3, 4, or 7."); } }
Of course, falling through like that in C++ is allowed, which was the point I was trying to make. I shouldn't have used empty cases to demonstrate that.Code:Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 Copyright (C) Microsoft Corporation. All rights reserved. Program.cs(16,17): error CS0163: Control cannot fall through from one case label ('case 3:') to another Program.cs(21,17): error CS0163: Control cannot fall through from one case label ('default:') to another
If you understand what you're doing, you're not learning anything.
Yeah. It's one of the limitations of how C# implements switches, I think. Not a pretty one... and one I confess I can't understand.
The switch statement is implemented through what is called a jump table in IL that is constructed from the list of offsets addresses that point to the initial instruction on every case block. However the block of instruction is well contained, because the jump table includes address offsets to every case statement. Here's the IL for istme86 code, after I added the break statements. If anyone can guess as to why these are required, I'd be happy to know:
It's not a pretty limitation, because it's the only language feature I know of on modern languages that will force you to use goto statements.
The limitation appears to me entirely compiler based and having nothing to do with any potential limitation of the IL. Why would they do that, completely beats me.Code:.method private hidebysig static void Main() cil managed { .entrypoint .maxstack 2 .locals init ( [0] int32 num, [1] int32 CS$4$0000) L_0000: nop L_0001: ldc.i4.7 L_0002: stloc.0 L_0003: ldloc.0 L_0004: stloc.1 L_0005: ldloc.1 L_0006: ldc.i4.3 L_0007: sub L_0008: switch (L_0023, L_0030, L_0048, L_0048, L_0030) L_0021: br.s L_0048 L_0023: ldstr "Doing something specific for 3 and falling through..." L_0028: call void [mscorlib]System.Console::WriteLine(string) L_002d: nop L_002e: br.s L_0055 L_0030: ldstr "The number was: " L_0035: ldloc.0 L_0036: box int32 L_003b: call string [mscorlib]System.String::Concat(object, object) L_0040: call void [mscorlib]System.Console::WriteLine(string) L_0045: nop L_0046: br.s L_0055 L_0048: ldstr "The number was not 3, 4, or 7." L_004d: call void [mscorlib]System.Console::WriteLine(string) L_0052: nop L_0053: br.s L_0055 L_0055: call string [mscorlib]System.Console::ReadLine() L_005a: pop L_005b: ret }
Last edited by Mario F.; 07-06-2011 at 03:18 PM.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Thank you all