C# 8 in VS 2019 – Setting up the development environment
The long-awaited next major version of the C# language (C# 8.0) is nearing its final release.
It’s going to be released at the same time as .NET Core 3.0. This means that just like .NET Core 3.0 preview, C# 8 is also included in the Visual Studio 2019 versions.
Editorial Note: The final version of Visual Studio 2019 got released today, April 2nd, 2019. However NET Core 3.0 is still in its preview as of this writing. .NET Core 3.0 will not be released along with VS 2019, but sometime later in 2019.
If you want to try out all the already available new language features yourself, you also need to install the latest preview version of .NET Core 3.0. That’s because some of the language features depend on .NET types which will be a part of .NET Standard 2.1.
At the time of writing, the only .NET platform with these types is .NET Core 3.0 (Preview 2 or later). It’s also worth mentioning that there are currently no plans for a future .NET framework version to implement .NET Standard 2.1 and include the new types required for some of the C# 8 features.
To create a suitable project for trying out all currently available C# 8.0 features, you can follow these steps:
- Create a new .NET Core project of any type.
- In the Application pane of the project Properties window, make sure that the Target framework is set to .NET Core 3.0.
- From the Build pane of the project Properties window, open the Advanced Build Settings dialog and select C# 8.0 (beta) as the Language version.
Nullable reference types
Nullable reference types were already considered in the early stages of C# 7.0 development but were postponed until the next major version. The goal of this feature is to help developers avoid unhandled NullReferenceExceptionexceptions.
The core idea is to allow variable type definitions to specify whether they can have null value assigned to them or not:
Assigning a null value or a potential null value to a non-nullable variable results in a compiler warning (the developer can configure the build to fail in case of such warnings, to be extra safe):
Similarly, warnings are generated when dereferencing a nullable variable without checking it for null value first:
The problem with such a change is that it breaks existing code: the feature assumes that all variables from before the change are non-nullable. To cope with that, static analysis for null-safety can be enabled selectively with a compiler switch at the project level.
Developers can opt-in for nullability checking when they are ready to deal with the resulting warnings. Still, this should be in their own best interest, as the warnings might reveal potential bugs in their code.
The switch is persisted as a property in the project file. There’s no user interface in Visual Studio 2019 yet for changing its value. Therefore, the following line must be added manually to the first PropertyGroup element of the project file to enable the feature for the project:
For more granularity, the #pragma warning directives can be used to disable and re-enable individual warnings for a block of code. As an alternative, a new #nullable directive has been added. It can be used to enable support for nullable reference types for a block of code even if it is disabled at the project level:
It’s a good idea to use #nullable restore instead of #nullable disable to disable nullable reference types for the code that follows. This will ensure that the checks remain enabled for the rest of the file if you later decide to enable the feature for the whole project. Using #nullable disable would disable the checks even in that case.
Improvements to pattern matching
Some pattern matching features have already been added to C# in version 7.0.
Several new forms of pattern matching are being added to C# 8.0.
Tuple patterns allow matching of more than one value in a single pattern matching expression:
The switch expression allows terser syntax than the switch statement when the only result of pattern matching is assigning a value to a single variable:
There are several differences in the syntax if we compare it to the switch statement:
- The left-hand variable of the assignment is specified only once before the expression, instead of in the body of each case.
- The switch keyword is placed after the tested value instead of placing it before it.
- The case keyword is not used anymore.
- The : character between the pattern and the body is replaced with a =>.
- Instead of break statements, the , character is used to separate the cases.
- For the body, expressions must be used instead of code blocks.
- For the catch-all case, a discard (_) is used instead of the default keyword.
A switch expression must always return a value. However, the code will still compile even if that’s not true (i.e. the cases do not cover all possible values):
The compiler will only emit a warning for the above code. If at runtime the tested value is not matched by any case, an InvalidOperationException will be thrown.
When testing a type with a Deconstructor method, positional patterns can be used which have syntax very similar to tuple patterns:
The code assumes that the Sword type contains the following Deconstruct method:
The pattern above compares the first deconstructed value and assigns the second deconstructed value to a newly declared variable. Although I’m using this pattern in an is expression, I could also use it in a switch expression or a switch statement.
Even if a type doesn’t have an appropriate Deconstruct method, property patterns can be used to achieve the same as with positional patterns:
The syntax is a bit longer but also more expressive. It’s a better alternative to a positional pattern in cases when the order of values in the Deconstruct method would not be obvious.
C# already has support for iterators and asynchronous methods. In C# 8.0, the two are combined into asynchronous streams. These are based on asynchronous versions of the IEnumerable and IEnumerator interfaces:
Additionally, an asynchronous version of the IDisposable interface is required for consuming the asynchronous iterators:
This allows the following code to be used for iterating over the items:
It’s very similar to the code we’re using for consuming regular synchronous iterators. However, it does not look familiar because we typically just use the foreach statement instead. An asynchronous version of the foreach statement is available for asynchronous iterators:
Just like with the synchronous foreach statement, the compiler will generate the required code itself.
It is also possible to implement asynchronous iterators using the yield keyword, similar to how it can be done for synchronous iterators:
You might have noticed the CancellationToken parameter of the GetAsyncEnumerator method of the IAsyncEnumerable<T> interface. As one would expect, it can be used to support cancellation of asynchronous streams.
However, there are currently no plans to support this parameter in compiler generated code. This means that you will need to write your own code for iterating over the items instead of using the asynchronous foreach statement if you want to pass the cancellation token to the GetAsyncEnumerator method.
Also, when implementing an asynchronous iterator with cancellation support, you will need to implement the IAsyncEnumerable<T> interface manually instead of using the yield keyword and relying on the compiler to do it for you.
Ranges and indices
C# 8.0 introduces new syntax for expressing a range of values.
The starting index of a range is inclusive, and the ending index is exclusive. Alternatively, the ending can be specified as an offset from the end:
The new type can be used as an indexer for arrays. Both ranges specified above will give the same result when used with the following snippet of code:
The new syntax can also be used to define:
An open-ended range from the beginning to a specific index:
An open-ended range from a specific index to the end:
An index of a single item specified as an offset from the end.
The use of ranges and indices is not limited to arrays. They can also be used with the Span<T> type:
Although that’s the full extent to which the Range type can be used with existing types in .NET Core 3.0 Preview 2, there are plans to provide overloads with Range-typed parameters for other methods as well, e.g.:
So far, no clear information has been given if these will be a part of .NET Core 3.0 and .NET Standard 2.1. They could be added in a later version.
The using statement is a great way to ensure that the Dispose method will be called on a type implementing the IDisposable interface when an instance gets out of scope:
In C# 8.0, the using declaration is available as an alternative:
The using keyword can now be placed in front of a variable declaration. When such a variable falls out of scope (i.e. the containing block of code is exited) the Dispose method will automatically be called.
This can be especially useful when multiple instances of types implementing the IDisposable interface are used in the same block of code:
The above code is much more readable and less error-prone than the equivalent code written with the usingstatement:
Static local functions
Local functions were introduced in C# 7.0. They automatically capture the context of the enclosing scope to make any variables from the containing method available inside them:
In C# 8.0, you can declare a local function as static. This will prevent using the variables from the containing method in the local function and at the same time avoid the performance cost related to making them available. A variable from the containing method can of course still be passed to the local function as a parameter:
Disposable ref structs
C# 7.2 added support for structs which must be allocated on the stack (declared with the ref struct keywords). They are primarily useful in high-performance scenarios which require non-managed access to continuous blocks of memory (Span<T> is an example of such a type).
Such types are subject to many restrictions. Among others, they are not allowed to implement an interface. This also includes the IDisposable interface making it impossible to implement the disposable pattern.
Although they still can’t implement interfaces in C# 8.0, they can now implement the disposable pattern by simply defining the Dispose method:
This is enough to allow the type to be used with the using statement (or the using declaration):
After a long anticipation, C# 8.0 is finally available in preview as part of Visual Studio 2019. Its final version will be released together with .NET Core 3.0.
Unlike all the versions of the language so far, not all features of C# 8.0 will be available in the .NET framework. Asynchronous streams and ranges depend on types which will only be added to .NET Core 3.0 and other .NET platforms implementing .NET Standard 2.1. As per the current plans, .NET framework will not be among them.
In my opinion, the most important new features of C# 8.0 are null reference types and improvements to pattern matching. I think so because they will help us write more reliable and readable code. Along with several smaller features, these will also be available in the .NET framework. This is a good enough reason to start using C# 8.0 even in .NET framework projects once it is released.