What’s New In C# 8?

13Jun, 2019

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.

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.

Index d1 = 2; // number 2 from beginning 
Index d2 = ^ 3; // number 3 from end 
string[] week = { 
"Monday", 
"Tuesday", 
"Wednesday", 
"Thursday", 
"Friday", 
"Saturday", 
"Sunday" 
}; 
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.
    //Old Style 
    var lastDay = week[week.Count -1]; // Sanday 
    //New Style 
    var lastDay = week[^1]; // Sanday
  • Get a range of elements.
    //Old Style 
    var days = week.ToList().GetRange(2, 3); // Wednesday,Thursday,Friday 
    //or 
    var days = week.Skip(2).Take(3); // Wednesday,Thursday,Friday 
    //New Style 
    var days = week[1.. ^ 2]; // Wednesday,Thursday,Friday 
    // [1..^2] mean 
    // 1 : skip elements from the begining until the index 1 
    // ^2 : skip 2 element from the end

Limitation

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.

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.

interface IPrintable { 
void Print(string header, string text); 
// New overloadvoid Print(Document doc) => Print(doc.Header, doc.Text); 
} 
class Printer: IPrintable { 
public void Print(string header, string text) { 
... 
} 
// Print(Document) gets default implementation 
}

Switch Expressions

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

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

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.

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.

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.

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

Here is an example,

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

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.

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.

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

For example, if you are doing something like,

string name = null; 
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 ??.

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

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

  • 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.
Rate this post

error: Content is protected !!