C# 7.0 and 7.1 support in ReSharper – Async main

Many developers have been wishing for an async main method to be able to use async/await constructs in console-based applications. The good news is that C# 7.1 introduces support for this (spec)! ReSharper (and Rider) do so, too.

Let’s continue our blog series about ReSharper support for C# 7.0 and C# 7.1 with an episode that covers how ReSharper handles this new keyword.

Async main

Very often, I would create a quick console application to test-drive some code. Problem was, when that code was Task-based or using async/await, the Main method of my application would look something like this: some bootstrapping code, calling a MainAsync method which would run Task-based code:

C#

1

2

3

4

5

6

7

8

9

publicstaticvoidMain(string[]args)

{

MainAsync(args).RunAsync().GetAwaiter().GetResult();

}

publicstaticasyncTask MainAsync(string[]args)

{

// ...

}

With C# 7.1, we can now create an async Main instead:

C#

1

2

3

4

publicstaticasyncTask Main(string[]args)

{

// ...

}

When we start writing any async/await construct in an existing Main method, ReSharper will offer to convert the method to async Main:

And if we use postfix templates, ReSharper will do it automatically for us:

ReSharper also comes with a code inspection that will let us know when there are multiple entry point candidates. Since only one entry point is allowed in an application, this is an easy way of detecting potential compiler issues upfront.

Now let’s get a little bit geeky. ReSharper’s built-in IL (Intermediate Language) viewer (ReSharper | Windows | IL Viewer) shows us the compiler magic that is going on behind the scenes, as well as an auto-generated, classic, non-async Main method – just like before C# 7.1.