Wednesday, February 11, 2015

C# vs Java : switch blocks

The switch statement works pretty much the same way in C# as it does in Java.
  • Can only use char, bool, int, string, or enum
  • The type used for case label must match that used in switch statement
  • Case sensitive when it comes to matching Strings
  • Case labels must be a constant expression
  • Can use a variable for the switch part (provided its value is set)
  • Case blocks share the same scope
  • The default case may be listed first
  • The body of the default case may be empty

In Java, you don't prefix the element name in the case part with the name of the enum.
public enum Days {
  Sat, Sun, Mon, Tue, Wed, Thu, Fri;
}
public class Klass {
  public static void main(String... args) {
    switch(Days.Tue) {
      case Tue:
        System.out.println("case Tue executed");
        break;
    }

    System.out.println("Finished");
  }
}
C#, on other hand, requires you to specify both the name of the enum and the name of the element you're matching on. Groovy behaves the same way.
enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};
public class Klass {    
  public static void Main() {    
    switch(Days.Sat) {
      case Days.Sat:
        System.Console.WriteLine("case Sat executed");
        break;
    }
  
    System.Console.WriteLine("Finished");
  }
}
See Also: C# Is Like Groovy, Not Java
C# allows you to use goto statements to jump between cases.
public class Klass {    
  public static void Main() {
    switch(3) {
      case 4:
        System.Console.WriteLine("case 4 executed");
        break;
      case 3:
        System.Console.WriteLine("case 3 executed");
        goto case 4;
    }
  

    System.Console.WriteLine("Finished");
  }
}
C# even allows you to specify a goto that jumps outside of the switch block.
public class Klass {    
  public static void Main() {
    switch(3) {
      case 3:
        System.Console.WriteLine("case 3 executed");
        goto outside;
    }
  

    System.Console.WriteLine("Finished");
    return;

    outside:
    System.Console.WriteLine("Finished (called from goto)");  
  }
}
Although goto is listed as one of Java's keywords, it will cause a compiler error if you try to use it.
public class Klass {
  public static void main(String... args) {
    goto msg; // compiler error: unexpect token "goto"

    System.out.println("skip me");

    msg:
    System.out.println("Hello World");
  }
}
Note: C# requires that the goto statement be in the same block of code as the label. Therefore the following will fail to compile.
public class Klass {    
  public static void Main() {
    {
      // No such label 'msg' within the scope of the goto statement  
      goto msg;
      System.Console.WriteLine("skip me");
    }

    {
      msg:
      System.Console.WriteLine("Hello World");
    }
  }
}
It is also an error to use a goto without a corresponding label.
public class Klass {    
  public static void Main() {
    // No such label 'msg' within the scope of the goto statement  
    goto msg;
    System.Console.WriteLine("skip me");

    System.Console.WriteLine("Hello World");
  }
}

C# does not allow case statements to fall through. If you tried to do the following in C# you'll get a compiler error.
public class Klass {    
  public static void Main() {
    switch(3) {
      case 3:
        System.Console.WriteLine("case 3 executed");
      case 2:
        System.Console.WriteLine("case 2 executed");
      case 1:
        System.Console.WriteLine("case 1 executed");
      default:
        System.Console.WriteLine("default case executed");
        break;
    }

    System.Console.WriteLine("Finished");    
  }
}
Java, however, allows this type of behavior.
public class Klass {
   public static void main(String... args) {
      switch(3) {
         case 3:
            System.out.println("case 3 executed");
         case 2:
            System.out.println("case 2 executed");
         case 1:
            System.out.println("case 1 executed");
      }

      System.out.println("Finished");
   }
}

No comments:

Post a Comment