C#/cshorp Switch Case Usage

Source: Internet
Author: User
Tags case statement switch case

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);

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.