What is the point of having async Main?

As you know, C # 7 allows you to make the Main () function asynchronous.

What benefits does it give? For what purpose can you use async Main instead of the usual?

+8
source share
1 answer

This is actually C # 7.1, which introduces the main async line.

The purpose of this method is in situations where the Main method calls one or more asynchronous methods. Prior to C # 7.1, you needed to introduce a specific ceremony for this main method, for example, to call these asynchronous methods using SomeAsyncMethiod().GetAwaiter().GetResult() .

Thanks to the ability to celebrate Main async simplifies this ceremony, for example:

 static void Main(string[] args) => MainAsync(args).GetAwaiter().GetResult(); static async Task MainAsync(string[] args) { await ... } 

becomes:

 static async Task Main(string[] args) { await ... } 

For a good record of using this feature, see C # 7 Series, Part 2: Async Main .

+18
source

Source: https://habr.com/ru/post/1271795/


All Articles