What's New In C# 8

After 18 years of existence, C# is still as young and "sharp" as ever. Indeed, by open-sourcing its compiler and design, Microsoft strengthened the position of C# in the market by making it available to all developers on GitHub.

Contributing and voting to set the language roadmap are the main contributors to its success in the last few years. Last month, the C# language team in Microsoft announced the release of the preview version of VS 2019 and .NET Core 3.0. The exciting thing about it is that this version includes the new C# 8 that we can play with to test some of the new features.

In this article, I'll walk you through some of the new features introduced in the 8th version of C# and show some use cases.

Ranges and Indices

C# 8 introduces two new types and operators for collections manipulation and indexing. Basically, we will have a more interesting and elegant way to index and slide collections.

New types - System.Index and System.Range.

New Operators - .. and ^.

Let's see some examples.

  1. Index d1 = 2; // number 2 from beginning  
  2. Index d2 = ^ 3; // number 3 from end  
  3. string[] week = {  
  4.     "Monday",  
  5.     "Tuesday",  
  6.     "Wednesday",  
  7.     "Thursday",  
  8.     "Friday",  
  9.     "Saturday",  
  10.     "Sunday"  
  11. };  
  12. Console.WriteLine($ "{week[d1]}, {week[d2]}"); // "Wednesday , Friday “  

The element selection is 0-based if you are counting from the beginning and 1-based if you are counting from the end.

What's New In C# 8 
  • Get the last element.
    1. //Old Style  
    2. var lastDay = week[week.Count -1]; // Sanday  
    3. //New Style  
    4. var lastDay = week[^1]; // Sanday  
  • Get a range of elements.
    1. //Old Style  
    2. var days = week.ToList().GetRange(2, 3); // Wednesday,Thursday,Friday  
    3. //or  
    4. var days = week.Skip(2).Take(3); // Wednesday,Thursday,Friday  
    5. //New Style  
    6. var days = week[1.. ^ 2]; // Wednesday,Thursday,Friday  
    7. // [1..^2] mean   
    8. //  1 : skip elements from the begining until the index 1  
    9. // ^2 : skip 2 element from the end  


This new feature will be available in any framework implementing .NET Standard 2.1. For example, it will be available in .NET Core 3.0 but not in the .NET framework 4.8. If you want more information about .NET Standard you can check this article.

Default Implementations of Interface Members

For this part, we will imagine that we have a three-year-old application with a bunch of classes and interfaces. Thirty of these classes implement an Interface called IPrintable. This interface includes a signature for each printer operation.

Let's say you want to add a new method that does exactly the same thing as "Print" but has a different signature. In this case, there is no way around reimplementing the new method for all 30 classes.

With C# 8, you can define a default implementation for the new interface member.

Let's check how this works.

  1. interface IPrintable {  
  2.     void Print(string header, string text);  
  3.     // New overloadvoid Print(Document doc) => Print(doc.Header, doc.Text);   
  4. }  
  5. class Printer: IPrintable {  
  6.     public void Print(string header, string text) {  
  7.         ...  
  8.     }  
  9.     // Print(Document) gets default implementation  
  10. }  

Switch Expressions

We can say that this is a light or short version of a switch statement with a new syntax using the => operator.

  1. var volume = shape  
  2. switch {  
  3.     Cube c => Math.Pow(length, 3),  
  4.         Prisme p => p.Width * p.Height * p.length,  
  5.         Cylinder cy => Math.PI * Mathf.Pow(cy.Radius, 2) * cy.heigth,  
  6.         _ =>  
  7.         throw new UnknownShapeException(shape)  
  8. };   

New Target-Typed Expressions

To build a collection with typed objects, we are obliged to specify the type for each element or use the new statement for the object construction.

This is an example.

  1. Month[] months = {new Month("January",31),new Month("April",30)};  

With the new target-typed expression, there's no need to specify the object type or class name and we can do it like this.

  1. Month[] months = {new(“January",31),new("April",30),new("June",31)};  

Async Streams

C# 5 made it possible to have some asynchronous features like awaiting a result with await and async. In C# 8, we get an evolution of this feature adding the IAsyncEnumerable which allows us to yield results asynchronously.

This feature can be very useful if you are developing an IoT app and you are dealing with a lot of asynchronous calls returning data.

  1. static async IAsyncEnumerable < string > GetElementsAsync() {  
  2.     await Task.Delay(2000);  
  3.     yield  
  4.     return new Element();  
  5. }  
  6. // we can use the await with a foreach too  
  7. await foreach(var name in GetNamesAsync()) {  
  8.     // do some stuff  
  9. }  

Here is an example,

We also need to add async to our Main method to make it work,

  1. static async Task Main(string[] args)  

Nullable Reference Types

As we all know there are two* types in C# : reference type and value type. struct is a typical example of a value type while class is a reference type. If you want more information about those two, check this link.

In this section, we will talk about the famous, the one, the only null pointer exception and how to prevent it from happening. It's too simple --  you need just to activate the null reference types feature in your project and you will get a warning if you are doing something wrong.

  1. // add this line to your .csproj  
  2. <NullableReferenceTypes>true</NullableReferenceTypes>  

For example, if you are doing something like,

  1. string name = null;  
  2. Console.WriteLine($"My name is {name}");  

You will get a first warning saying "Assigning null to a non-nullable type."

We will fix it by changing the string to string and checking if the name is null with ??.

  1. string? name = null;  
  2. Console.WriteLine($"My name is {name ?? '?'}");  

You can get his type of warning if you are using Resharper too :p

  • C# includes 3 types, not two. I discovered the third one this week; it's called "Pointer Type". I've never used this type but it looks like the C/C++ pointer.


To follow the evolution of C#, you can follow those GitHub repositories :

  • https://github.com/dotnet/csharplang
  • https://github.com/dotnet/roslyn

Similar Articles