C# 7.0 and 7.1 support in ReSharper – Async main

ReSharper support for C# 7 and C# 7.1Many 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.

In this series (we will update the table of contents as we progress):

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:

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

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

Inspection makes Main method async

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

Postfix templates automatically convert Main method to use async

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.
More than one entry point defined with async main

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.
IL viewer async main method

Download ReSharper Ultimate or check out Rider that takes advantage of ReSharper’s language support updates. We’d love to hear your thoughts.

This entry was posted in How-To's and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *