C# 8 is the upcoming latest major version of the official programming language from Microsoft. C# 8 is packed with amazing features and some of them have the potential to change the way developers will write their C# code in the future. Let me introduce my top three features!
How to enable C# 8 in Visual Studio 2019
C# 8 is currently under active development, but a lot of the functionalities are already available in preview. However, when you install Visual Studio 2019, C# 8 is not available by default, so you need to explicitly enable it to start playing with the new features.
Right click on your project, click Properties, click Build, click Advanced and then select “unsupported preview of next C# version (preview)” in the Language Version.
Some of the new features introduced in the language require .NET Core 3, so you want to install the latest .NET Core 3 SDK and enable previews of the .NET Core SDK in Visual Studio 2019.
In the menu bar, click Tools, click Options, expands Projects and Solutions, click .NET Core and check Use previews of the .NET Core SDK.
Create a new console application targeting .NET Core 3 and you are now ready to play with C# 8!
1. Nullable Reference Types
Even if you specify C# 8 as your language version in Visual Studio, this C# feature is disabled by default.
To enable the feature, you need to add the NullableContextOptions tag in your C# project file with the value set to enable.
Enabling this feature drastically changes the semantics of your code.
The compiler will start working with a different set of rules.
Any variable of a reference type will be considered non-nullable and can be dereferenced safely. The compiler will show a warning all the time you attempt to assign null to a reference type variable.
If you want a variable of a reference type to be nullable you need to explicitly add a question mark after the type in the variable declaration. For these variables, the compiler will perform a flow analysis and show a warning if the value is known to be null when it is dereferenced.
The new set of warnings the compiler can generate, can be a massive help for the developers and will help to spot bugs and write code of better quality.
This feature is going to be a game changer for C# developers.
2. Switch Expression with pattern matching
The switch statement in C# was completely inherited from the C programming language and it has always been very verbose.
C# 8 introduces a new streamlined way to write conditional code as an expression.
The image below shows how you can rewrite an old switch statement using a switch expression. You can clearly see how compact and easier to read is the new code.
The switch expression has some limitations (by the fact that is an expression and must always return a value) so you can’t replace all usages of switch statements in your projects. However, in most cases, the replacement can be done, and I would recommend you to always use the new syntax over the old one when possible.
The switch expression also support a new set of patterns like property patterns, tuple patterns and positional patterns that help you write complex pattern matching scenarios in a few lines of code.
3. Async Streams
The IEnumerable<T> interface, extension methods and LINQ were one of the major innovations introduced in C# 3. Its empowered developers to write C# code in a compact and more declarative way and significantly increased our productivity. This is by far, my favourite feature of the entire C# language!
C# 5 introduced the async and await pattern to write asynchronous code in a more intuitive way, making it looks like writing synchronous code. C# was the first language that introduced this feature and it was yet another major innovation of modern programming language design.
C# 8 finally merged the power of these two features, adding the ability to easily implement asynchronous streams of data and easily iterate through them asynchronously using a familiar foreach loop.
This is now possible by the introduction of the IAsyncEnumerable<T> and IAsyncDisposable<T> interfaces.
The following image shows you how you can implement a method that return the content from different web pages as an async stream of data.
You can then consume the stream of data using an await foreach.
This code is extremely intuitive for the regular C# developer.
The amount of code generated by the compiler under the covers to implement the asynchronous behavior is a lot, but all the additional complexity is hidden from you. This lets the compiler do the hard work so we can focus on writing our application logic.
Nullable reference types, switch expressions and async streams will change the way we write C# code in the future. With C# 8, the language will increase our productivity, help us write code that is even more compact and easier to understand and perhaps more importantly, it will help to write more robust code that will work correctly at run-time.
Say goodbye to the nasty NullReferenceException! 😊
Do you want to get an in-depth understanding of C# 8 and all the new features? Make sure you register to the next C# 8 webinar from Productive C#.
Happy C# Coding!