Async Main in Console App Visual Studio

Subscribe to my newsletter and never miss my upcoming articles

Many times we want to create some Pocs for our project wherein we test out other libraries, frameworks, NuGet packages, etc., and a lot of these would be implementing asynchronous programming. Which means, to call these methods exposed by the libraries, NuGet packages, etc. you would have to do a await in your console app. And as you are aware, Main method of Console App needs to be a static void Main(string[] args) and if you try to make the Main method async, you would get the error : Error-on-trying-async-1.png

There are many workarounds for making the async calls from Main method, but I like to keep the Main method async and do an await inside it just like any other method. And this is now possible with C# version greater than 7.1

To do so, Right click on your project and select Properties and navigate to the Build Tab. There, click on Advanced button at the bottom. Advanced-1.png

In the Advanced menu, for the Language Version chose any c# version greater than 7.1 7.1-1.png

In case you can't see C# 7.1 or greater, please install any pending updates to your Visual Studio. And now you can change the Main method to :

 static async Task Main(string[] args)

and do the await inside it. async-1.png

No Comments Yet