swich case

This is a discussion on swich case within the C# Programming forums, part of the General Programming Boards category; hello.I'm new to c# and I want to know if there is any difference in "switch case" between c++ and ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    swich case

    hello.I'm new to c# and I want to know if there is any difference in "switch case" between c++ and c#?

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    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.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    look, somewhere in my program I have written the code below:

    Code:
      enum Direction
        {
            North,
            South,
            East,
            West,
            Up,
            Down,
            Here
        };
    and later in my program I have written the following code:
    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;
                }
    it draws a red line under North, South,...giving this error:
    The name 'North' does not exist in the current context.
    I have no clue what is wrong with that.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    if there is any difference in "switch case" between c++ and c#?
    I read somewhere that in C# switch, there is no fall-through for the cases .

    DISREGARD - Was wrong. <Thank: Magos>
    Last edited by manasij7479; 07-06-2011 at 06:30 AM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,501
    Quote Originally Posted by nvoigt View Post
    The most important difference is that in C# you can "switch" on strings.
    additionally, I believe that you can switch on anything that implements the equality operator, whether implicitly or explicitly.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Quote Originally Posted by Magos View Post
    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;
    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.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Sorry, you can fall through empty cases, but if you try something like this:
    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.");
                }
            }
    ... you get (notice it doesn't complain about case 4) ...
    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
    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.
    If you understand what you're doing, you're not learning anything.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    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.

    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 
    }
    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.
    Last edited by Mario F.; 07-06-2011 at 03:18 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Smile

    Thank you all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  2. case ' ':
    By jaylc185 in forum C Programming
    Replies: 5
    Last Post: 06-08-2005, 05:28 PM
  3. help with swich cases
    By digdug4life in forum C++ Programming
    Replies: 15
    Last Post: 02-15-2005, 07:17 PM
  4. Swich/Case Question
    By Xzyx987X in forum C Programming
    Replies: 4
    Last Post: 05-05-2004, 02:26 PM
  5. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21