How to Use switch case Statements in C#
This tutorial describes how to use switch case statements in C#.
The switch statement chooses one of the blocks of code, which matches the switch expression to execute.
We often use the switch-case statements when we test a single expression against three or more conditions. You can use if-else if there are only a few conditions.
Related C# tutorial:
The switch case Statements Syntax
The syntax of the switch and case statements is as follows:
switch(expression) { case a: //block of code break; case b: //block of code break; case c: //block of code break; ......... default: //block of code break; }
Remarks
- The expression can be a value of char, bool, enum, string, or an integral value, such as a long or an int.
- By using the break keyword, C# breaks out of the switch statement when there is a match and stops the execution from reaching the next switch. Only one switch section in a switch statement executes.
- The block of code inside the default section executes if the expression doesn't match any other case. The default section is optional.
Examples
Example 1
You can use the switch statement, as the following example shows:
using System; namespace SwitchCaseExample { class Program { static void Main(string[] args) { int number = 2; switch (number) { case 1: Console.WriteLine("A"); break; case 2: Console.WriteLine("B"); break; case 3: Console.WriteLine("C"); break; case 4: Console.WriteLine("D"); break; default: Console.WriteLine("E"); break; } } } }
Output:
B
The program outputs "B" because the expression matches case 2.
Example 2
The following illustrates an example of the execution of a block of code inside the default section:
using System; namespace SwitchCaseExample { class Program { static void Main(string[] args) { int number = 10; switch (number) { case 1: Console.WriteLine("A"); break; case 2: Console.WriteLine("B"); break; case 3: Console.WriteLine("C"); break; case 4: Console.WriteLine("D"); break; default: Console.WriteLine("E"); break; } } } }
Output:
E
The code block inside the default section executes because the expression doesn't match any other case.
Example 3
The following example shows how to use the enum type in the switch statement:
using System; namespace SwitchCaseExample { class Program { static void Main(string[] args) { DayOfWeek dayOfWeek = DayOfWeek.FRIDAY; PrintDayOfTheWeek(dayOfWeek); } static void PrintDayOfTheWeek(DayOfWeek dayOfWeek) { switch (dayOfWeek) { case DayOfWeek.MONDAY: Console.WriteLine("Today is Monday."); break; case DayOfWeek.TUESDAY: Console.WriteLine("Today is Tuesday."); break; case DayOfWeek.WEDNESDAY: Console.WriteLine("Today is Wednesday."); break; case DayOfWeek.THURSDAY: Console.WriteLine("Today is Thursday."); break; case DayOfWeek.FRIDAY: Console.WriteLine("Today is Friday."); break; case DayOfWeek.SATURDAY: Console.WriteLine("Today is Saturday."); break; case DayOfWeek.SUNDAY: Console.WriteLine("Today is Sunday."); break; } } } public enum DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY, } }
Output:
Today is Friday.
More C# tutorials:
In this tutorial, you have learned how to use switch-case in the C# programming language. You can use switch and case when you need to test a single expression against three or more conditions. However, if there are only a few conditions, you should use if and else statements instead.