C# 8.0 new features

C# 8.0 new features

23 December 2020

C#_8.0_new_features_01

C# 8.0 is Microsoft’s seventh major update to the flagship programming language. It gives unusual flexibility and breadth to the C# as a language. At one end, it offers abstractions of high-level such as asynchronous continuations and query expressions, whereas at the other end, through constructs such as custom value types and optional pointers it allows low-level efficiency.

C# 8  – What’s New

Nullable Reference Types:

We all know that there are two types in C# : value type and reference type. A typical example is struct which is a value type while an example of reference type is class. First, we will discuss the famous, null pointer exception and how to prevent it from occurring.

It’s very simple — we just need to activate the feature of null reference types in our project and we will get a warning if we are doing something wrong with the code.

For example, if we are doing something like this:

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

First, we will get a warning saying “Assigning null to a non-nullable type. We can fix it by changing the string to string? and check if the name is null with ??.

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

Async Streams:

We have asynchronous features like awaiting a result with await and async since the release of C# 5. In C# 8, this feature got an evolution of adding the IAsyncEnumerable which allows us to yield results asynchronously.

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

Here is an example,

static async IAsyncEnumerable<string> GetElementsAsync()
{
  await Task.Delay(3000);
  yield return new Element();
}

//await with for each loop
await foreach (var name in GetNamesAsync())
{//some working code }

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

static async Task Main(string[] args)

New Target-Typed Expressions:

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

With this new target-typed expression we will not need to specify the object type or class name we can do like this:

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

Ranges and Indices:

Two new types and operators for collections manipulation and indexing are introduced. Basically, to index and slide collections we will have a more interesting and elegant way.

New types: System.Index and System.Range and,

New Operators: .. and ^

  • Get the last element:

    //Old Style
    var lastDay = week[week.Count -1]; // Sanda
    
    //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

Conclusion

The C# compiler is delivering on the promise of Roslyn: faster introduction of new features thanks to a completely new codebase.

At the same time, for the larger teams, new features are not forced. They can have stricter control over the language version they are using. New features can be evaluated and decided at their own pace when they want to adopt them.

To comply with the open-source model, anybody can explore or even contribute their opinion because the state of upcoming features in future versions of the language is public and available for all. It’s important to keep in mind though, that these features are still a work in progress and as such, they could change without warning or even be postponed to a later version.

search

Blog Categories

Request a quote