C# 8.0 New Feature Switch Expressions

In this article, we are going to learn what Switch Expressions are in C# and how to use switch expressions in C# 8.

Introduction

 
In this article, we are going to learn a new feature of C# 8.0 - Switch Expressions.
 

What is a Switch Expressions?

 
In C# 7.0, the Switch statements are more powerful with patterns. In Switch expressions, all the cases are expressions so it's a more 'lightweight' version. In the below code, anyone can analyze what exactly it is doing. 
  1. var operation = 2;  
  2.   
  3. var result = operation switch  
  4. {  
  5.     1 => "Case 1",  
  6.     2 => "Case 2",  
  7.     3 => "Case 3",  
  8.     4 => "Case 4",  
  9. };  
  10.   
  11. Console.WriteLine(result);  
In the above code, anyone can find that there is no 'case', 'break'(or return) statement. In Switch expressions, these keywords are not necessary. The colon(:) is replaced with the arrow(=>). It is nothing but expressions, everything after the arrow is an expression. One more keyword is there, i.e., "default". It's also replaced with the "_"(underscore).
 
The below code shows all the cases with the default.
  1. var operation = 2;  
  2.   
  3. var result = operation switch  
  4. {  
  5.     1 => "Case 1",  
  6.     2 => "Case 2",  
  7.     3 => "Case 3",  
  8.     4 => "Case 4",  
  9.     _ => "No case availabe"  
  10. };  
  11.   
  12. Console.WriteLine(result);  
It is also used for property assignment or various ways we can use a Switch expression. To see the various ways to use Switch expressions, we are creating the simple console application in Visual Studio 2019 because these features are available in the latest Visual Studio version. 
 
Currently, Visual Studio 2019 Preview is available that you can download from here.
 
Following are the steps to create a new console application in Visual Studio 2019 Preview.
  1. Open Visual Studio 2019 Preview. 
  2. Select Create a new project => Select Console App (.Net Core) => Provide project name => Click Create button.
  3. Once the console app is created then next we need to set the latest C# version.
  4. Right, click on your project => Select Properties => Once the property window is open => Click on Build from the left menu.
  5. Scroll down to right side page => Click on Advanced button => Advanced Build Settings popup is opened with default language version.

    C# 8.0 New Feature Swtich Expressions

  6. Click on language version dropdown => select C# 8.0 (beta).

    C# 8.0 New Feature Swtich Expressions

  7. Click on save changes.
  8. Open the Program.cs file, copy and paste the following code,
    1. using System;    
    2. using System.Collections.Generic;    
    3.     
    4. namespace CSharp8.NewFeature.SwitchExpressions    
    5. {    
    6.     class Program    
    7.     {    
    8.         static void Main(string[] args)    
    9.         {    
    10.             Console.WriteLine("C# 8.0 New Feature Swtich Expressions");    
    11.     
    12.             //example1    
    13.             var (a, b, option) = (10, 5, "+");    
    14.     
    15.             var example1 = option switch    
    16.             {    
    17.                 "+" => a + b,    
    18.                 "-" => a - b,    
    19.                 _ => a * b    
    20.             };    
    21.             Console.WriteLine("Example 1 : " + example1);    
    22.     
    23.             //example2    
    24.             var cal = new Calculation(10, 20, "/");    
    25.             var example2 = cal.Operation switch    
    26.             {    
    27.                 "+" => cal.FirstNumber + cal.SecondNumber,    
    28.                 "-" => cal.FirstNumber - cal.SecondNumber,    
    29.                 _ => cal.FirstNumber * cal.SecondNumber,    
    30.             };    
    31.             Console.WriteLine("Example 2 : " + example2);    
    32.             Console.WriteLine("Property Assignment : " + cal.LogLevel);    
    33.     
    34.             //example3    
    35.             var value = 25;    
    36.     
    37.             int example3 = value switch    
    38.             {    
    39.                 _ when value > 10 => 0,    
    40.                 _ when value <= 10 => 1    
    41.             };    
    42.             Console.WriteLine("Example 3 : " + example3);    
    43.     
    44.             //example4    
    45.             var dic = new Dictionary<stringstring>();    
    46.             var (key, defaultValue) = ("Jeetendra""C# Corner");    
    47.     
    48.             var example4 = dic.TryGetValue(key, out string val) switch    
    49.             {    
    50.                 false => defaultValue,    
    51.                 _ => val    
    52.             };    
    53.             Console.WriteLine("Example 4 : " + example4);    
    54.         }    
    55.     }    
    56.     
    57.     public class Calculation    
    58.     {    
    59.         public Calculation(int a, int b, string operation)    
    60.         {    
    61.             this.FirstNumber = a;    
    62.             this.SecondNumber = b;    
    63.             this.Operation = operation;    
    64.         }    
    65.     
    66.         public int FirstNumber { getset; }    
    67.     
    68.         public int SecondNumber { getset; }    
    69.     
    70.         public string Operation { getset; }    
    71.     
    72.         public int LogLevel { get; } = Environment.GetEnvironmentVariable("LOG_LEVEL"switch    
    73.         {    
    74.             "INFO" => 1,    
    75.             "DEBUG" => 2,    
    76.             _ => 0    
    77.         };    
    78.     }    
    79.     
    80. }    
  9. After copying and pasting the code, save changes and click on Run.
  10. The output of the above code you can see in the below screenshot. 
C# 8.0 New Feature Swtich Expressions 
 
You can also download this console application from here
 

Summary

In this article, we discussed what is Switch Expressions in C# 8.0 and how to use it. If you have any suggestions or queries regarding this article, please contact me.

Stay tuned for other concepts of C# 8.0.

“Learn It, Share it.”