Asynchronous Main Method in C# 7.1
As we are aware all .NET based applications will be having an entry point and .NET runtime will load and start executing the application from here.
Since C# 5.0, we are able to use async/await pattern such that respective method/class will be executed asynchronously. But, this pattern is still not able to apply to the main entry method I.e.., static void main. Until C# 7.0, if you want to achieve an async entry point we use to do as follows.
public class AsynchronousMainMethod { public static void Main(string[] args) { MyMainAsync(args).GetAwaiter().GetResult(); } public static async Task MyMainAsync(string[] args) { //Do Some Logic here. } }
Now from C# 7.1, we can directly decorate async keyword to the main entry point. The above code will be as follows in C# 7.1.
public class AsynchronousMainMethod_7_1 { public static async Task Main(string[] args) { //Do Some Logic here. } }
Actually in C# 7.1, behind the scene, the compiler creates a non-async Main method as a wrapper to support backward computability.
Happy Coding 🙂