The latest C# release marks an important stage in the evolution of one of the best-known programming languages. You can expect some long-awaited additions, as well as a few interesting developments.
With C# 8.0, you can now use pattern-matching, nullable reference types, readonly numbers, declarations, as well as a few other notable features. Whether you’ve been using C# for years or are considering making the transition to it, now it’s a good time to take a quick look at what it has to offer.
So, without further ado, let’s explore what’s new in C# 8.0 and how the new features can help developers and software development companies develop better software. We’ll focus on the big features, the ones that can really make a difference.
Nullable Reference Types
Nullable reference types have finally made it into C#. Variable type definitions can now carry a null value – that means that developers can specify whether a definition can or can’t be null to make their design intent clearer.
To declare a nullable reference type, you have to add ? to the type. For example, string is not nullable by default. But you can make it nullable if you add ? to it – string?.
Nullable types help you better handle NullReference exceptions, which have been a nuisance for many developers.
However, using null values for non-nullable valuable will likely generate a compiler warning. That said, you can use null-safety static analysis to deal with warnings.
To use nullable reference types, you need the C# 8.0 compiler available on .NET Core 3.0 and Visual Studio 2019. You can use nullable reference types for both new projects and existing apps.
Pattern Matching Improvements
With the new release, C# extends the pattern expressions you can use to implement shape-dependent functionality for different types of data.
Sounds a bit complicated? It doesn’t have to be. Let’s put it this way – of all C# releases so far, it’s the one that comes closest to enabling you to separate functionality from data.
Let’s take a closer look at some of the most important new patterns C# 8.0 introduces.
- Tuple patterns enable you to bundle multiple values into a single object so that you can switch between them with greater ease. Unlike a simple variable, a tuple contains two or more values (up to eight) and may also include arrays.
- Positional patterns can be used in conjunction with a Deconstruct method to handle object properties much in the same way as you would treat a pattern. For example, you can use them to extract values and determine Quadrants.
- Property patterns come in handy when you want to match on properties for an object. They work with types that lack a Deconstruct.
- Switch expressions allow for shorter code than the switch statements that have been available until now. Because of this, they often make the code easier to understand (provided you’re used to Lambda expressions). In other words, switch expressions do away with long syntaxes to return values quicker.
Overall, you can look at C# 8.0’s pattern-matching improvements as a continuation of those C# 7.0 introduced. They give you more flexibility without complicating the code.
For a more in-depth look at pattern-matching enhancements in C# 8.0, check out this page.
Readonly numbers are properties that cannot alter the state of the instance they are used with. In simple, practical terms, this means they help you avoid changing internal states when it’s not needed.
It’s one of the C# 8.0 enhancements that enable you to make the intent of your code clearer right from the start.
You can add the readonly modifier to any member of a structure to indicate that it does not alter state.
Null Coalescing Assignments
You can now use ??= as a null-coalescing operator to check whether object properties are null. Going a step further, you can use null coalescing to assign a value to an object (if it is null).
When it’s implemented effectively, it makes the assignment logic for parameter default value more to the point.
At the same time, it can help you avoid system bugs that are the result of simply copy-pasting code without updating the relevant parts.
You can now create and return asynchronous streams using the async modifier. To consume an asynchronous stream, C# 8.0 lets you add await before foreach when listing the different stream elements.
Asynchronous streams essentially combine the existing iterators and asynchronous methods with which you may already be familiar with. They’re a particularly useful feature if you want to consume or produce continuous streams of results.
Asynchronous streams can help with coding cloud-based services or Internet of Things (IoT) software.
Indices and Ranges
You can now access ranges or single elements within a sequence using a more concise syntax.
The new types introduced for this purpose are System.Index, which is essentially an index into a sequence, and System.Range, a sequence sub-range.
You can now also use two new operators: ^, which specifies the index from the end, and .., which specifies the beginning and end of a range.
To find out more about the indices and ranges rules, check out Microsoft’s explanation.
Default Interface Methods
One of the most notable features in C# 8.0, default interface methods enable you to add members to interfaces. By doing this, you can implement methods into an API even in a later version without breaking the compatibility with existing implementations.
Thanks to the new default interface methods, developers can now evolve an interface safely without having to fall back on a workaround like an extension method, which has its limitations.
Default interface methods won’t force clients to implement the new methods. They can stick to the default one if they choose.
The latest version of C# 8.0 is arguably the most powerful C# release to date. More than addressing some old issues and concerns, it can help your team develop better-quality software more intuitively and effectively.
The new features that are most likely to impact quality include nullable reference types, readonly properties, and the new pattern matching improvements, among others. These additions help developers to focus on the code that really matters instead of just getting lost in lengthy and complicated syntaxes.
Overall, C# 8.0 is not likely going to disappoint you. On the contrary, some of its features may pleasantly surprise you once you get used to them.
Whether you want to build Windows desktop apps or other software, the new release provides functionality without sacrificing accessibility. That said, to take advantage of its full potential, you want to make sure you have a C# 8.0 expert on your team.