O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

11.4. Conditional statements with the switch andcase keywords

C# has made one efficient change for the switch clause to accept strings in addition to integral expressions. The switch statement in Java can only accept the byte, short, char, and int primitive types.

Examine the following program which uses switch to compare a string (ignore the goto statement for now, it will be explained later).

 1:  using System;
 2:
 3:  class TestClass{
 4:    public static void Main(string []args){
 5:      string s = args[0];
 6:
 7:      switch(s){
 8:            case "jam" : Console.WriteLine("BUTTER");
 9:                         goto case "butter";
10:
11:            case "cake": Console.WriteLine("CAKE");
12:                         break;
13:
14:            case "bug" : Console.WriteLine("BUG");
15:                         break; 16: 17: case "fly" : Console.WriteLine("FLY"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required