C# - Top 10 New Features (Part 1 Of 4)

Article Overview

  • Background
    • How to compile/test with C# 8.0 in MS Visual Studio OR online
    • Key features and enhancements to C# 8.0
    • Switch Expression feature
  • Pre-requisites
  • How to compile/test with C# 8.0 in MS Visual Studio OR online
    • MS Visual Studio
    • Online tool
  • Key features and enhancements to C# 8.0
    • Switch Expressions
    • Indices and Ranges
    • Default Interface Members
    • Static Local Functions
    • Using Declarations
    • Nullable Reference Types
    • Readonly Struct Members
    • Null-Coalescing Assignment
    • Async Streams
    • Property, Tuple, and Positional Patterns
  • Switch Expression feature
    • Points
    • Example
  • Summary

Background

 
There was a situation where I had to know all the key important features and enhancements in C# 8.0. There are a number of articles and blogs where you can find detailed explanations of new features of C# 8.0.
 
Hence, to understand each one in detail, I went through various sites instead of one single place. So, here I am putting all the key details along with key points as well as examples with code in one place. This way, you can get most of all information from a single place.
 
I have divided this whole article into 4 parts for making it easy to understand with step by step movement to go ahead.
 
This article mainly focuses on three key things.
  • How to compile/test with C# 8.0 in MS Visual Studio OR online
  • Key features and enhancements to C# 8.0
  • Switch Expressions
Here, I have kept all the implementation details along with a complete example.
 
Prerequisites
 
You should have a basic knowledge of C#.

How to compile/test with C# 8.0 in MS Visual Studio OR online

 
First of all, before you start, you have to make the environment ready which can compile your C# 8.0 code.

There are two ways of doing this.
  • MS Visual Studio
  • Online tool 
MS Visual Studio

Go to “Advanced Build Settings” and set “Language version” to “C# 8.0 (beta)”.
 
 
Online

There are various online tools where you can write, compile, and execute your code online. I am using “C# Online Compiler | .NET Fiddle”  where you have to set “Compiler” to “.NET Core 3.0”.
  

Key features and enhancements to C# 8.0

 
C# 8.0 added the following key features and enhancements to the C# language.
  • Switch Expressions
  • Indices and Ranges
  • Default Interface Members
  • Static Local Functions
  • Using Declarations
  • Nullable Reference Types
  • Readonly Struct Members
  • Null-Coalescing Assignment
  • Async Streams
  • Property, Tuple, and Positional Patterns
Now, let us discuss each feature one by one in detail.
 
For your reference, I have kept all the examples in a single .cs file and uploaded them with this article.
 

Switch Expression feature

 
Below are the key syntax improvements:
  • Bodies are expressions, not statements.
  • Case and : elements are replaced with =>. It is more concise and intuitive.
  • Variable comes before the switch keyword. It is visually easy to distinguish the switch expression from the switch statement.
  • Default case is replaced with a _ discard.
Now, let us understand by examples.

Example-1
  1. var (a, b, option) = (10, 5, "+");
  2.      
  3. var example1 = option switch     
  4. {     
  5.         "+" => a + b,     
  6.         "-" => a - b,     
  7.         _ => a * b     
  8. };
  9.      
  10. Console.WriteLine(example1);  
Example-2
  1. var value = 25;    
  2.   
  3. int example2 = value switch      
  4. {      
  5.         _ when value > 10 => 0,      
  6.         _ when value <= 10 => 1      
  7. };
  8.       
  9. Console.WriteLine( example2);  
Example-3
  1. Calculation c1 = new Calculation(50, 10, "*");  
  2. c1.print();  
  3.   
  4. Calculation c2 = new Calculation(50, 10, "+");  
  5. c2.print();  
  6.    
  7.   
  8. public class Calculation      
  9. {      
  10.     public Calculation(int a, int b, string operation)      
  11.     {      
  12.         this.FirstNumber = a;      
  13.         this.SecondNumber = b;      
  14.         this.Operation = operation;    
  15.           
  16.         this.ResultNumber = this.Operation switch      
  17.         {      
  18.                 "+" => this.FirstNumber + this.SecondNumber,      
  19.                 "-" => this.FirstNumber - this.SecondNumber,      
  20.                 "/" => this.FirstNumber / this.SecondNumber,      
  21.                 "*" => this.FirstNumber * this.SecondNumber,      
  22.                 _ => -1     
  23.         };  
  24.     }      
  25.   
  26.     public int FirstNumber { getset; }      
  27.   
  28.     public int SecondNumber { getset; }      
  29.   
  30.     public string Operation { getset; }      
  31.   
  32.     public int ResultNumber { getset; }      
  33.       
  34.     public void print()  
  35.     {  
  36.         Console.WriteLine($"{this.FirstNumber} {this.Operation} {this.SecondNumber} = {this.ResultNumber}");    
  37.     }  
  38. }  

Summary

 
Now, I believe you will be able to compile/test with C# 8.0 in MS Visual Studio OR online, you know the key new features and enhancements to C# 8.0, and you understand the Switch Expression feature.
 
In my next article, I will cover the next three features such as "Indices and Ranges", "Default Interface Members", and "Static Local Functions". 


Recommended Free Ebook
Similar Articles