Switch upon enum value in CSharp

Description

The following code shows how to switch upon enum value.

Example


using System; /*from ww w. ja va  2 s .co  m*/
 
class ConveyorControl { 
  public enum action { start, stop, forward, reverse }; 
 
  public void conveyor(action com) { 
    switch(com) { 
      case action.start: 
        Console.WriteLine("Starting conveyor."); 
        break; 
      case action.stop: 
        Console.WriteLine("Stopping conveyor."); 
        break; 
      case action.forward: 
        Console.WriteLine("Moving forward."); 
        break; 
      case action.reverse: 
        Console.WriteLine("Moving backward."); 
        break; 
    } 
  } 
} 
 
public class MainClass {     
  public static void Main() { 
    ConveyorControl c = new ConveyorControl(); 
 
    c.conveyor(ConveyorControl.action.start); 
    c.conveyor(ConveyorControl.action.forward); 
    c.conveyor(ConveyorControl.action.reverse); 
    c.conveyor(ConveyorControl.action.stop); 
     
  } 
}

The code above generates the following result.





















Home »
  C# Tutorial »
    C# Language »




C# Hello World
C# Operators
C# Statements
C# Exception