C# 8.0 Features: Should a Developer Use It?
It’s been almost 20 years since Microsoft released the first version of the C# language. Until now with the release of C# 8, the language has had a remarkable evolution.
There are a lot of new features Microsoft has introduced in their latest release of C# which is worth trying out.
The major ones to be listed are:
1. Default interface methods
2. Nullable reference types
3. Pattern matching enhancements
4. Asynchronous streams
5. Using declarations
6. Enhancement of interpolated verbatim strings
7. Null-coalescing assignment
8. Static local functions
9. Indices and ranges
10. Unmanaged constructed types
11. Read-only Struct Member
12. Stackalloc in nested expressions
13. Disposable ref structs
Let us go through each one of them and understand what these features are for.
1. Default interface methods
It allows you to add new functionality to the interfaces of your libraries and ensure backward compatibility with code written for older versions of those interfaces.
2. Nullable reference types
Emits a compiler warning or error if a variable that must not be null is assigned to null.
3. Pattern matching enhancements
Provides the ability to deconstruct matched objects, and gives you access to parts of their data structures. C# offers a rich set of patterns as below that can be used for matching:
a. Switch expressions
A switch expression is a concise way to return a specific value based on another value.
A regular switch does not return a value. This syntax is more concise. There are no case keywords, and the default case was replaced with a discard ( _ ).
The case conditions can be patterns. It’s not possible to include statements for handling a case. For each case, a single expression must be provided that represents the resulting value. This expression can be a switch expression.
b. Property patterns
Property patterns express a property that needs to have a specific constant value.
The above statement will match when location, Country equals India. Property patterns can be used in switch expressions also. This pattern can also be used with the is keyword.
We can check both on the type and property, for example:
C. Tuple patterns
The conditions for the switch are now also tuples. Their items have patterns that are matched against the corresponding input tuple element. In the example, we’ve used the discard (_) to ignore a tuple item. Other patterns can also be used.
The is keyword can also be used with tuple patterns. Tuple patterns can also be used against types that are deconstructable to a tuple:
d. Positional patterns
If we 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 looks much similar to that of tuple patterns.
If we look at the pattern closely, we 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.
4. Asynchronous streams
Allows having enumerators that support async operations.
5. Using declarations
It enhances the ‘using’ operator to use with Patterns and makes it more natural.
6. Enhancement of interpolated verbatim strings
Allows @$”” as a verbatim interpolated string.
7. Null-coalescing assignment
Simplifies a common coding pattern where a variable is assigned a value if it is null. It is common to see the code of the form.
8. Static local functions
Allows you to add the ‘static’ modifier to the local functions.
Here as we are using the parent variables, the runtime will copy these variables into SayHello() stack memory. It means we are copying these variables to local variables. In our case, we are passing only the name string variable to the local function.
9. Indices and ranges
Allows you to use more natural syntax for specifying subranges in an array or a collection.
Index: Used to obtain the collection from the beginning or from the end.
Range: Access a sub-collection(slice) from a collection.
10. Unmanaged constructed types
Allows you to take a pointer to unmanaged constructed types, such as ValueTuple, as long as all the elements of the generic type are unmanaged.
11. Read-only struct member
You can now apply read-only to any struct members. This is an extension of adding to read-only as a struct level. It indicates that the member does not alter the data or modify the state. This feature helps the compiler in not creating a defensive copy of a variable.
12. Stackalloc in nested expressions
The result of a stackalloc expression is of the System.Span or System.ReadOnlySpan type.
13. Disposable ref structs
Allows us to use the ‘using’ pattern with ref struct or readonly ref struct‘.
Conclusion
C# 8 has many new useful features, which are well accepted among the developer community. These feature additions can make your code better, safer, cleaner and easily maintainable. If so, why don’t we use it? Happy coding.