C# 8.0 Preview


The guys from Microsoft seem to be working with lightning speed. At least when it comes to C# and the .Net framework.

Looks like C# 8.0 is at our doorstep and while it’s still early news it’s exciting nevertheless. In a recent channel Channel 9 video Mads Torgersen has demonstrated the first few features that we can expect.

Nullable Reference Types

The reference types would no longer be nullable by default. Instead, you would have to explicitly mark them as nullable using the same “Type?” syntax that you use for nullable value types.

Assigning a null to a non-nullable reference type will be a compiler warning. Likewise, reading from a nullable type would be a compiler warning unless the variable in question was explicitly checked for null ahead of time. So developers would need to put question marks where appropriate:


Async Streams

Async streams(or foreach async) is a feature that has been talked about for a while now and we might finally get it in C# 8.0. The syntax is supposed to be:

foreach await (string s in asyncStream)

And when defining an async iterator, you would use this function signature:

async IAsyncEnumerable MethodName()

Default Interface Implementations

Default interface implementations are basically a (limited) form of multiple-inheritance. This will allow abstract interfaces to fully define methods just like abstract classes. However, abstract interfaces will still not be able to declare constructors or fields.

The primary benefit of default interface implementations is that you would be able to add new methods to an existing interface without breaking backwards compatibility (sort of, because this isn’t guaranteed, as it would only work when a suitable default method can be devised).

Extension Everything

So far we’ve only been able to add extension methods in static classes. With this new feature we would finally be able to define extension properties as well as well as not have to use static classes to define your extension methods(which was ‘sort of’ a complaint by many people).

Under the new design, there is a new top-level construct called an “extension”. For example, if you want to create extension methods and properties for a Customer class you would write:

extension ProductExt extends Product {
//methods and properties go here

In addition to properties, events, and operator overloads, they are even considering allowing extension constructors (could be very interesting in factory and object pooling scenarios).

More info

For more information, you can watch the Channel9 video with Mads Torgersen .

You can also follow the progress of the C# language design in the official C# language repo on GitHub.

‘Till next time…Happy coding.