Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

C# 8.0 to have async streams, recursive patterns and more

Save for later
  • 4 min read
  • 14 Nov 2018

article-image

C# 8.0 will introduce some new features and will likely ship out the same time as .NET Core 3.0. Developers will be able to use the new features with Visual Studio 2019.

Nullable reference types in C# 8.0


This feature aims to help prevent the null reference exceptions that appear everywhere. They have riddled object-oriented programming for half a century now.

The null reference exceptions stop developers from using null in ordinary reference types like string. They make these types non-nullable. They are warnings, however, not errors. On existing code, there will be new warnings. Developers will have to opt into using the new feature at the project, file, or source line level. C# 8.0 will let you express your “nullable intent”, and throws a warning when you don’t follow it.

string s = null; // Warning: Assignment of null to non-nullable reference type

string? s = null; // Ok

Asynchronous streams with IAsyncEnumerable<T>


The async feature that was from C# 5.0 lets developers consume and produce asynchronous results. This is in straightforward code, without callbacks.

This isn’t helpful when developers want to consume or produce continuous streams of results. For example, data from an IoT device or a cloud service. Async streams are present for this use.

C# 8.0 will come with IAsyncEnumerable<T>. It is an asynchronous version of the existing IEnumerable<T>. Now you can await foreach over functions to consume their elements, then yield return to them in order to produce elements.

async IAsyncEnumerable<int> GetBigResultsAsync()

{

   await foreach (var result in GetResultsAsync())

   {

       if (result > 20) yield return result;

   }

}

Ranges and indices


A type Index is added which can be used for indexing. A type index can be created from an int that counts from the beginning. It can be alternatively created with a prefix ^ operator that counts from the end.

Index i1 = 3;  // number 3 from beginning

Index i2 = ^4; // number 4 from end

int[] a = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Console.WriteLine($"{a[i1]}, {a[i2]}"); // "3, 6"


C# 8.0 will have an added Range type consisting of two Indexes. One will be for the start and one for the end. They can be written with an x..y range expression.

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at €18.99/month. Cancel anytime

Default implementations of interface members


Currently, once an interface is published members can’t be added anymore without breaking all its existing implementers. With the new release, a body for an interface member can be provided. If somebody doesn’t implement that member, the default implementation will be available instead.

Allowing recursive patterns


C# 8.0 will allow patterns to contain other patterns.

IEnumerable<string> GetEnrollees()

{

   foreach (var p in People)

   {

       if (p is Student { Graduated: false, Name: string name }) yield return name;

   }

}


The pattern in the above code checks that the Person is a Student. It then applies the constant pattern false to their Graduated property to see if they’re still enrolled. Then checks if the pattern string name to their Name property to get their name. Hence, if p is a Student who has not graduated and has a non-null name, that name will yield return.

Switch expressions


Switch statements with patterns a powerful feature in C# 7.0. But since they can be cumbersome to write, the next C# version will have switch expressions. They are a lightweight version of switch statements, where all the cases are expressions.

Target-typed new-expressions


In many cases, on creating a new object, the type is already given from context. C# 8.0 will let you omit the type in those cases.

For more details, visit the Microsoft Blog.

ReSharper 18.2 brings performance improvements, C# 7.3, Blazor support and spellcheck

Qml.Net: A new C# library for cross-platform .NET GUI development

Microsoft announces .NET standard 2.1