A C#/cshorp switch statement is a control statement that handles multiple selections and enumerations by passing control to a case statement within its body
int caseswitch = 1;
Switch (Caseswitch)
{
Case 1:
Console.WriteLine ("Case 1");
Break
Case 2:
Console.WriteLine ("Case 2");
Break
Default
Console.WriteLine ("Default case");
Break
}
A case statement that matches the value of a switch is passed to the system. A switch statement may include any number of case instances, but no two cases statements can have the same value. The statement body begins execution from the selected statement until the break passes control to the case body. After each case block (including the previous block, whether it is a case statement or a default statement), you must have a jump statement (such as a break). With one exception (unlike C + + switch statements), C # does not support the explicit penetration of a case label into another case label. This exception is when there is no code in the case statement
Using System;
public enum TimeOfDay {
Morning = 0,
Afternoon = 1,
Evening = 2
}
Class Enumexample {
public static void Main () {
Writegreeting (timeofday.morning);
}
static void Writegreeting (TimeOfDay TimeOfDay) {
Switch (TimeOfDay) {
Case timeofday.morning:
Console.WriteLine ("Good morning!");
Break
Case Timeofday.afternoon:
Console.WriteLine ("Good afternoon!");
Break
Case timeofday.evening:
Console.WriteLine ("Good evening!");
Break
Default
Console.WriteLine ("Hello!");
Break
}
}
}
Working with Functions Www.111cn.net
Using System;
public enum Week {Monday, Tuesday, Wednesday, Thursday, Friday, saturaday,sunday};
Class Conveyorcontrol {
public void conveyor (Week com) {
Switch (COM) {
Case Week.monday:
Console.WriteLine ("Starting Monday.");
Break
Case Week.tuesday:
Console.WriteLine ("Stopping Tuesday.");
Break
Case Week.wednesday:
Console.WriteLine ("Moving Wednesday.");
Break
Case Week.thursday:
Console.WriteLine ("Moving Thursday.");
Break
}
}
}
Class MainClass {
public static void Main () {
Conveyorcontrol C = new Conveyorcontrol ();
C.conveyor (Week.thursday);
C.conveyor (Week.tuesday);
C.conveyor (Week.wednesday);
C.conveyor (Week.monday);
}
}