Article Overview

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

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".