C# 7.1 : New Features With Example


Recently, Microsoft released a new version of C# which is C# 7.1 with Visual Studio 2017 update 3. It is a minor release so it has few features. The features are async Main method, default, infer tuple name and pattern matching with generics. This article intends to explain these features with examples.


Configure C# Language Version in Visual Studio

By default, Visual Studio configures with a major version of C#. We configure Visual Studio for C# 7.1 language using below steps.

  1. Right click on the project. After that click on Properties at the bottom.
  2. It opens a new window in Visual Studio. Now click on Build from the left menu.
  3. Now click on the Advance button at bottom of this Build pane. It is greeted by a pop-up window.
  4. Now pick language version in Advanced Build Settings. We pick C# 7.1 and clicks on OK button.

Figure 1: Language Configuration in C#


Async Main Method

We can develop two kinds of programs in C#. One is a program with entry point i.e Main method so that operating system can load and execute it from the entry point. There are several kinds of applications which have an entry point such as ASP.NET, ASP.NET Core App, Windows App, Console App etc. Another is a program without entry point. These programs can’t execute directly by operating system while these can be referenced by other programs with an entry point and code can be executed. These applications are Class Library, Module, Shared Library.

The C# 7.1 has a new feature that is an async main method. An async type method calls in another method using await. The await keyword uses in the async type method only. Suppose we have an async method and calls in the Main method using await then the Main method must be async. The following code snippet for same.


Default Literal

By default, The reference type variables hold a null value while value type variables hold a default value. Default literal is similar to null literal but it works for both value type and reference type. The default literal uses to get a default value of specified type.

In the earlier version of C#, we use default literal with a type such as default(T) but in the new version, we just use default keyword only. As it omits the type so it is shorter form. Internally, It still works same as default(T) and implicit convert to any type. Let’s see an example.

All reference types have null as default value. We can assign a null value to the reference types but the default is more readable.


Infer Tuple Names

Tuple features are enhanced in the new release. The article Tuple In C# 7.0 introduces tuples new features in C# 7. This new feature allows that the tuple element names be omitted and instead be inferred. We can say that tuple element names are same as assigned property to a tuple element. It is similar to anonymous types. Let’s see an example for anonymous type.

As per preceding example, we create an anonymous type named newEmployee. It creates from Employee class object and assigns properties of that. It doesn’t have any property name implicitly. Its properties names are inferred. This anonymous type has same property names which are in Employee class and used to create it.

Similar way, tuple can be used. Tuple has some advantages over anonymous type which are.

  1. Tuple is struct so it can be used to improve performance.
  2. Tuple can be returned from a method and can be used as a parameter in methods.
  3. We can define extension method using Tuple.

Let’s see an example for a tuple.

As per preceding example, we create a tuple named newEmployee. It creates from Employee class object. It doesn’t have element name implicitly. Its element names are inferred. This tuple has same element names which are in Employee class and used to create it.


Pattern Matching With Generics

C# 7.0 was introduced pattern matching. A detailed article about pattern matching in C# is Pattern Matching In C#. It enhanced in C# 7.1 with generic types as well. Let’s see an example.

The preceding example doesn’t compile with the C# 7.0. The generic pattern matching has been implemented in Area() method of the Program class. This example uses the switch pattern and matches with the compatible type. The Area method has a generic type as a parameter. Passed parameter implicit cast in compatible type and expression executes accordingly.


Conclusion

C# 7.1 is minor release and Microsoft C# team is working on C# 8. There are four new features expected in C# 8 which are

  1. Nullable reference type: Suppose we declare a string variable and doesn’t assign any value then its value is null. Now we use Length property of it so it throws an error. In this case, VS will show warnings to take appropriate actions. for example string? name= “Sandeep”;
  2. Async Stream: Send IEnumerable data as a stream asynchronously.
  3. Default implementation of interface: It is almost same as an abstract class but supports to multiple inheritance. The interface would have both method signature and definitions.
  4. Extension Everything: It enhanced from extension method. It is similar to partial classes but these can be extended in the different assembly. Properties and classes can be extended.