Finally, after a long wait, the latest version of the C# language, C# 8.0 has been released. Whether you are a web development company into enterprise software development, these new C# 8 features matter a lot for your future credentials. The release of C# 8.0 as expected has coincided with the release of the .NET Core 3.0. Naturally, just like the preview of the .NET Core 3.0 the latest C# 8 has become a part of the new Visual Studio 2019.

Here through the length of this post we are going to explain the new C# 8 features in Visual Studio. But before that let us explain how to make C# 8 active in Visual Studio 2019.

How to enable C# 8 in Visual Studio 2019?

C# 8 as the latest version of the C# language is still undergoing development but the developers can access a lot of features that are available through preview. It is important to note that the Visual Studio 2019 does not provide access to C# 8 features by default. You need to enable the C# 8 for getting access to all these features. Let us explain the steps in brief.

Start by clicking right on the project and then click Properties followed by Build and then Advanced. Now you need to select the option entitled “unsupported preview of next C# version (preview)” among the available Language Versions.

Just go to the menu bar and then click on Tools. Now click on Options and then under Projects and Solutions you find .NET Core. Click on it and check the box beside Use previews of the .NET Core SDK.

Now, you are ready to utilise all the latest features of the C# 8.

Let us now explain the key features of the C# 8.

  • Nullable Reference Types

Now, we have already seen how to enable the latest C# 8 version in Visual Studio. Let’s focus on the features.

When for enabling C# 8 You add the NullableContextOptions tag in your present C# project file having a value set to enable, the code semantics completely changes and the compiler starts working with different rules.

The variable of any reference type is regarded as non-nullable and the difference can be drawn out quite safely. You are always on the safe side as you always receive a warning from the compiler as and when you add a null to the variable reference.

For creating variables of the reference types that are nullable you are required to add question marks to the type in your variable declaration. A separate flow analysis will be performed for such variables and a warning will be issued in case the value is still null in spite of boasting a difference.

All the newer warnings that compiler from time to time generates can actually help developers tremendously for detecting bugs and ensuring better code writing. This feature is going to play a massive role for the C# developers making Asp.net development services easy and successful.

  • Improvements to pattern matching

An array of pattern matching features added to the C# 8 is a key value addition to the programming language.

Let’s have a brief look at several new types of pattern matching that are available C# 8.0.

  • Tuple patterns

Tuple patterns are known to facilitate number of values within a single expression of pattern matching. Let’s see how Tupelo patterns look.

Source:https://www.dotnetcurry.com/csharp/1489/csharp-8-visual-studio-2019/
 
  • Switch expression

Switch expression is another key addition to favour terser syntax instead of the switch statement in all those cases where the objective of the pattern matching is to assign a value to only one variable.

Source:https://www.dotnetcurry.com/csharp/1489/csharp-8-visual-studio-2019/
 

In this respect we must remember that there is an array of differences in the syntax when we compare the same to the switch statement.

Source:https://www.dotnetcurry.com/csharp/1489/csharp-8-visual-studio-2019/
 

For the code code as mentioned above, compiler can just issue a warning. In case the tested value does not match at runtime, it will show an InvalidOperationException.

  • Positional patterns

If you prefer to try testing following the Deconstructor method, these positional patterns will be more ideal for the purpose. These patterns boast of a syntax that look much similar to that of tuple patterns.

Source:https://www.dotnetcurry.com/csharp/1489/csharp-8-visual-studio-2019/
 

If you look at the pattern closely, you will see that the primary deconstructed value and the second deconstructed value are compared to a new variable. Apart from this expression, the same can be used in a switch statement and sweet expression.

  • Property patterns

Even when a particular type doesn’t follow any proper Deconstruct method, the use of property patterns is quite appropriate to achieve similar results as we get by using positional patterns.

With property patterns the code syntax looks somewhat longer, though the syntax is also more expressive than others . It can be used alternatively with positional pattern. It  is great particularly for all those cases of Deconstruct method where there is no obvious order of values.

  • Async Streams

Writing asynchronous code in C# 8 evolved over the years. Right from the C# 3 where features like IEnumerable<T> interface, extension methods and LINQ helped developers writing code in a more compact way to the C# 5 that featured the async and await pattern to help developers write asynchronous code in more intuitive fashion, C# made several value additions to coding.

We are fortunate that in C# 8 these two features are merged for more powerful, intuitive and expressive coding output. In the new version, it is much easier  now to deploy asynchronous data streams of data and easily make asynchronous iteration through them by using just a foreach loop.

The image below showcases how by implementing a method you can actually get access to the contents of a variety of web pages as the asynchronous data stream.

The image below shows how after accessing the data stream you can consume them by using the foreach loop.

This feature makes the code truly intuitive as per the earlier standard of C# code used by the developers. In spite of producing a bulk size of code with asynchronous attributes, the code created by the compiler at surface comes without much of the complexities. This feature actually makes the compiler to do all the hard work while the developers can actually concentrate on the core aspects such as application logic.

  • using declarations

A using declaration comes as the variable declaration that directs the compiler to dispose the declared variable when the enclosing scope arises. Look at the  example below.

IMG

Source:https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8
 

While in the above instance, the disposal of the file happened as and when the method is reached for the closing brace. It shows the end of the scope for the declaration of the file. On the other hand, the following code does the same by using the traditional “using statements” statement:

IMG

Source:https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8
 

In both the above-mentioned cases, a call to Dispose is generated by the compiler. In case the expression of the statement cannot be disposed, an error warning is produced by the compiler.

  • Ranges and indices

Another big value addition to the C# 8.0 is that it introduced new syntax to express a whole array of values. For a range the index of the beginning remains inclusive while the index of the end is exclusive. In alternate expressions, the ending can always be specified as an offset pertaining to the end.

In the new syntax the type can also be used as the indexer for arrays and both the above-mentioned ranges will offer the same output as and when used with a code snippet mentioned below.

var array = new[] { 0, 1, 2, 3, 4, 5 };

var subArray = array[range];

The new C# 8 syntax can also be utilised for defining the open-ended range right from the start to a particular index.

var subArray = array[..^1];

Here is an image of an open-ended range representing a particular index to the end:

var subArray = array[1..];

An index of a single item specified as an offset from the end.

The new syntax elements such as ranges and indices of the C# 8 is not just limited to the arrays. The utilisation of these syntax elements also comprises Span<T> type (you can read more about Span<T> in my DNC Magazine article C# 7.1, 7.2 and 7.3 – New Features).

var array = new[] { 0, 1, 2, 3, 4, 5 };

var span = array.AsSpan(1, 4);

var subSpan = span[1..^1];(1, 4);

  • Disposable ref structs

The support for the structs were first introduced by the C# 7.2. They were deemed as truly useful for all high-performance scenarios and access to continuous memory blocks.

Though structs cannot implement interface in C# 8.0, they now can implement various disposable patterns with this version.

Source:https://www.dotnetcurry.com/csharp/1489/csharp-8-visual-studio-2019
 

This is enough to allow the type to be used with the using statement (or the using declaration):

Source:https://www.dotnetcurry.com/csharp/1489/csharp-8-visual-studio-2019
 

Conclusion

C# 8 came with a lot of new features to boost productivity and efficiency. The new C# 8.0 will help developers write code with more compactness and run-time precision.