C# 7.1 New Features
With Visual Studio 2017.3, came the first "point" release or minor update to C#, 7.1. This release adds three new features on top of C#7.0: async main method, default literal expressions and inferred tuple element names.
In this post, I will cover how to enable C# 7.1 and the new features which came with it.
How to select C# version
By default, the 7.1 features are turned off. To enable 7.1 features, you need to change the language version setting of the project in Visual Studio.
Right-click the project, select properties. Go to Build tab and click Advanced button:
Select C# latest Minor version:
Async Main
Now you can make the Main entry point method async, and return Task or Task<int>.
Earlier, if you called an async method from the Main method, you had to call it synchronously by calling method GetAwaier().GetResult() on it.
static void Main()
{
AsyncMethod().GetAwaiter().GetResult();
}
Now, you can make the Main method async and await any async call inside:
static async Task Main()
{
await AsyncMethod();
}
Default Literal Expressions
Assigning default value has been simplified, let's see how:
Before C# 7.1:
bool hasValue = default(bool);
Action<int, bool> action = default(Action<int,bool>);
Now, you don't need to specify the type on the right-hand side of initialization:
bool hasValue = default;
Action<int, bool> action = default;
Inferred Tuple Element Names
Tuples were enhanced in C# 7.0 release, check out what changed in this post.
In C# 7.1, tuple element names can be inferred when it is initialized:
int low = 1;
int high = 10;
var a = (low, high);
Is same as declaring:
(int low, int high) t = (low, high);