.NET core, C# and the Async Main() method

The more I mess around with C# the more I like it.
The nice thing about .NET Core is that it really is cross platform. I can start writing something on my Linux box, continue on Mac and finish it on Windows and use the same editor along the way – VS Code.

One of the coolest parts of C#, at least for me, is the async part. It really is quite easy to make web or any other type of IO requests asynchronous.
But one thing I did not know until today was that as of C# 7.1 you can also make your Main() method async.

When I started learning C# a while back I read that Main() must return void or int, therefore it cannot be async as that would require returning T or T<>.

This can be a small issue when you write a CLI app and you want to call an async method from within your Main().
So what you’d normally do in such situation is that you’d use the awaiter like so:

  static void Main (string[] args) {
      DisplayTempAsync().GetAwaiter().GetResult();
  }

This will work, but it is not too pretty.
However as it turns out C# 7.1 now supports async Main() methods and you can now write your code like so:

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

This is way nicer, and in order for it to work you must specify C# 7.1 version in your csproj file, like so:

 
    Exe
    netcoreapp2.1
    7.1
  

Though it’s worth sharing.