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.

Top 3 Features in C# 8 that will change the way you write code 2
Enable C# 8 in Visual Studio 2019

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

Top 3 Features in C# 8 that will change the way you write code 3
Enable .NET Core SDK preview features in Visual Studio 2019

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.

Top 3 Features in C# 8 that will change the way you write code 4
Enable NullableContextOptions

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. 

Top 3 Features in C# 8 that will change the way you write code 5
C# as you know it versus Nullable Reference Types Enabled

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.

Top 3 Features in C# 8 that will change the way you write code 6
New Switch expression compared to existing syntax

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.

Top 3 Features in C# 8 that will change the way you write code 7
New Switch expression syntax

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. 

Top 3 Features in C# 8 that will change the way you write code 8
IAsyncEnumerable used with yield return

You can then consume the stream of data using an await foreach.

Top 3 Features in C# 8 that will change the way you write code 9
Easily consume returned IAsyncEnumerable

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.

Conclusion

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#.

Top 3 Features in C# 8 that will change the way you write code 10

Happy C# Coding!

Advertisements

Posted by Andrea Angella

Owner of My Productive C#, the only membership site completely focused on C# Software Development. My productive C# offers an extensive collection of practical C# videos, new videos added weekly, a curated weekly newsletter, discounts for productivity tools, a private mastermind group and direct mentoring from Andrea Angella. My Productive C# is the perfect place for anyone looking to become a master and successful C# developer. So check it out at http://productivecsharp.com/membership

One Comment

  1. […] Top 3 Features in C# 8 that will change the way you write code (Andrea Angella) […]

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.