C# switch case statement is a selection statement. It executes code of one of the conditions based on a pattern match with the specified match expression. The switch statement is an alternate to using the if..else statement when there are more than a few options. The code examples in this article demonstrate various usages of switch..case statement in C# and .NET Core.
 
The switch statement pairs with one or more case blocks and a default block. The case block of code is executed for the matching value of switch expression value. If the switch value doesn’t match the case value, the default option code is executed.
 
The following is the definition of the switch..case statement. 
  1. switch (expression)  
  2. {  
  3.    case expression_value1:  
  4.       Statement  
  5.    break;  
  6.    case expression_value2:  
  7.       Statement  
  8.    break;  
  9.    case expression_value3:  
  10.          Statement  
  11.    break;  
  12.    default:  
  13.       Statement  
  14.    break;  
  15. }  
The expression in the above code can be any non-null expression.
 
Listing 1 demonstrates a typical switch statement. The switch expression is a random number between 1 and 9. Based on the value of the expression, a case block is executed. If the value of a switch expression is doesn’t match with first three case values, the default block is executed. 
  1. // Generate a random value between 1 and 9  
  2. int caseSwitch = new Random().Next(1, 9);  
  3. switch (caseSwitch)  
  4. {  
  5.    case 1:  
  6.       Console.WriteLine("Case 1");  
  7.    break;  
  8.    case 2:  
  9.       Console.WriteLine("Case 2");  
  10.    break;  
  11.    case 3:  
  12.       Console.WriteLine("Case 3");  
  13.    break;  
  14.    default:  
  15.       Console.WriteLine("Value didn’t match earlier.");  
  16.    break;  
  17. }  
Listing 1.
 
The case statement can be a statement of one or multiple statements or nested statements.
 
Listing 2 uses multiple statements in case 1. 
  1. switch (caseSwitch)  
  2. {  
  3.    case 1:  
  4.       Console.WriteLine("Case 1");  
  5.       DateTime date = DateTime.Today;  
  6.       Console.WriteLine("Today's date is {0}", date);  
  7.       if (date.Day == 2)  
  8.       {  
  9.          Console.WriteLine("This is the shortest month");  
  10.       }  
  11.    break;  
  12.    case 2:  
  13.       Console.WriteLine("Case 2");  
  14.    break;  
  15.    case 3:  
  16.       Console.WriteLine("Case 3");  
  17.    break;  
  18.    default:  
  19.       Console.WriteLine("Default case");  
  20.    break;  
  21. }  
Listing 2.
 
Using Enum in a switch statement
 
Let’s find if today is a week end or a week day. Listing 3 uses an enum in a case statement and checks if the DayOfWeek is Saturday or Sunday, it’s a weekend, else it’s a work day. 
  1. // switch..case with enum  
  2. void WeekEndOrWeekDay()  
  3. {  
  4.    switch (DateTime.Now.DayOfWeek)  
  5.    {  
  6.    case DayOfWeek.Saturday:  
  7.    case DayOfWeek.Sunday:  
  8.       Console.WriteLine("Today is Weekend");  
  9.    break;  
  10.    default:  
  11.       Console.WriteLine("Today is a work day.");  
  12.    break;  
  13.    }  
  14. }  
Listing 3.
 
Using multiple case in one switch
 
You can execute the same code for multiple switch expression values. In Listing 4 example, if the value is Color.Blue, Color.Black, Color.Orange, or default, the last line of code is executed. 
  1. public enum Color { Red, Green, Blue, Black, Orange }  
  2. public static void RandomConsoleBackground()  
  3. {  
  4.    Color c = (Color)(new Random()).Next(0, 4);  
  5.    switch (c)  
  6.    {  
  7.    case Color.Red:  
  8.       Console.BackgroundColor = ConsoleColor.Red;  
  9.       Console.Clear();  
  10.       Console.WriteLine("Red");  
  11.    break;  
  12.    case Color.Green:  
  13.       Console.BackgroundColor = ConsoleColor.Green;  
  14.       Console.Clear();  
  15.       Console.WriteLine("Green");  
  16.    break;  
  17.    case Color.Blue:  
  18.    case Color.Black:  
  19.    case Color.Orange:  
  20.    default:  
  21.       Console.WriteLine("No need to change background.");  
  22.    break;  
  23. }  
Listing 4.
 
Case Patterns
 
The case statement defines a pattern to match the match expression. There are two types of patterns, constant pattern, and non-constant (type) pattern.
 
Constant pattern
 
The constant pattern tests whether the match expression equals a specified constant. In case of a constant pattern, the case statement is followed by a constant value. 
  1. case constant:  
where constant is the value to test for and can be any of the following constant expressions,
  • A bool literal, either true or false.
  • Any integral constant, such as an int, a long, or a byte.
  • The name of a declared const variable.
  • An enumeration constant.
  • A char literal.
  • A string literal. 
The constant expression is evaluated as follows,
  • If match expression and constant are integral types, the equality operator ‘==’ is used to compare the value and returns true for the matching value.
  • Otherwise, the value of the expression is determined by a call to the static Object.Equals method.
Listing 5 is an example of using a char constant in a switch statement. 
  1. // switch..case with char  
  2. void CharSwitchCase()  
  3. {  
  4.    char ch = 'c';  
  5.    switch (ch)  
  6.    {  
  7.    case 'c':  
  8.       Console.WriteLine("c found!");  
  9.    break;  
  10.    default:  
  11.       Console.WriteLine("c not found!");  
  12.    break;  
  13.    }  
  14. }  
Listing 5.
 
Listing 6 is an example of using a string constant in a switch statement. 
  1. // switch..case with string  
  2. void StringSwitchCase()  
  3. {  
  4.    string name = "Mahesh";  
  5.    switch (name)  
  6.    {  
  7.    case "Mahesh":  
  8.       Console.WriteLine("First name was used!");  
  9.    break;  
  10.    case "Chand":  
  11.       Console.WriteLine("Last name was used!");  
  12.    break;  
  13.    default:  
  14.       Console.WriteLine("No name found!");  
  15.    break;  
  16.    }  
  17. }  
Listing 6.
 
Type Pattern
 
The switch statement can use a type as an expression. 
  1. case type varname  
where type is the name of the type to which the result of expr is to be converted, and varname is the object to which the result of expr is converted if the match succeeds. 
  • The case expression is true if any of the following is true:
  • expr is an instance of the same type as type.
  • expr is an instance of a type that derives from type. In other words, the result of expr can be upcast to an instance of type.
  • expr has a compile-time type that is a base class of type, and expr has a runtime type that is type or is derived from type. The compile-time type of a variable is the variable's type as defined in its type declaration. The runtime type of a variable is the type of the instance that is assigned to that variable.
  • expr is an instance of a type that implements the type interface. 
The following code example in Listing 7 uses a type to compare with an enum, an Array, and a List as an expression in the switch..case statement.
  1. using System;   
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class Author  
  5. {  
  6.    string name;  
  7.    string book;  
  8.    int year;  
  9.    public Author(string Name, string Book, int Year)  
  10.    {  
  11.       this.name = Name;  
  12.       this.book = Book;  
  13.       this.year = Year;  
  14.    }  
  15. }  
  16. public enum Color { Red, Green, Blue, Black, Orange }  
  17. class SwitchCaseExample{  
  18. static void Main(string[] args)  
  19. {  
  20.    // Pass an Enum  
  21.    SwitchCaseWithTypePatternSample(Color.Red);  
  22.    // Pass an Array  
  23.    string[] names = {"Mahesh Chand""Allen O'neill""David McCarter"};  
  24.    SwitchCaseWithTypePatternSample(names);  
  25.    // Pass a List  
  26.    List<Author> authors = new List<Author>();  
  27.    authors.Add(new Author("Mahesh Chand""ADO.NET Programming", 2002));  
  28.    SwitchCaseWithTypePatternSample(authors);  
  29.    Console.ReadKey();  
  30. }  
  31. public static void SwitchCaseWithTypePatternSample(object type)  
  32. {  
  33.    switch (type)  
  34.    {  
  35.       case Enum e:  
  36.       switch (e)  
  37.       {  
  38.          case Color.Red:  
  39.             Console.BackgroundColor = ConsoleColor.Red;  
  40.             Console.WriteLine("Red");  
  41.          break;  
  42.          case Color.Green:  
  43.          Console.BackgroundColor = ConsoleColor.Green;  
  44.          Console.Clear();  
  45.          Console.WriteLine("Green");  
  46.          break;  
  47.          case Color.Blue:  
  48.          case Color.Black:  
  49.          case Color.Orange:  
  50.          default:  
  51.             Console.WriteLine("No need to change background.");  
  52.          break;  
  53.       }  
  54.    break;  
  55.    case Array a:  
  56.       Console.WriteLine($"Number of items in array: {a.Length}");  
  57.    break;  
  58.    case IList list:  
  59.       Console.WriteLine($"Number of items in list: {list.Count}");  
  60.    break;  
  61.    default:  
  62.       Console.WriteLine("default");  
  63.    break;  
  64.    }  
  65. }  
  66. }  
Listing 7.
 
Summary
 
This article and code examples demostrates various use cases of C# switch..case statement.