C# 7.1 New Features

Starting with version 7, C# has moved to smaller set of changes being released more frequently. Version 7.1 introduces 3 new features, which are described below. You will need Visual Studio 2017 15.3 or later. In addition, you will need to specify the C# version in the project.

To modify the C# version for a project, go to the project properties, then click advanced in the build tab, from there-- you can select a specific version or select latest minor version.

async Main

The biggest change in C# 7.1 is the addition of async Main method. Main is the entry point to c# console app. The main method must be static, return int or void, and it does not need to be public. If we wanted to have an async starting point we would have to write a helper method.

static void Main(string[] args) => MainAsync(args).GetAwaiter().GetResult();

static async Task MainAsync(string[] args)
   await DoWork();

Now we can mark the main method as async.

static async Task Main(string[] args)
    await DoWork();

The Main method can now also either return Task or Task<int> when it's marked async.

Inferred tuple names

In C# 7 tuples got the ability to have named fields.

var result = (min: min, max: max);

In C# 7.1 names can be inferred based on the names of the values given.

var result = (min, max);

Default literal expressions

The default expression can now infer the type, so we can omit passing it.

public int DoSomething(int n = default(int))

Previously we would have to specify default(int) now we can simply put default.

public int DoSomething(int n = default)


Popular posts from this blog

Electron JS, Visual Studio Code, and SQL Operations Studio

Unity vs. Unreal Engine

Free & Paid Resources for Getting Started in IT