Back to: C#.NET Tutorials For Beginners and Professionals
Async Main in C# with Examples
In this article, I am going to discuss the Async Main in C# with Examples. Please read our previous article where we discussed the Thrown Expression in C# with Examples. From C# 7.1 now it is possible to define the Main Method as Async. At the end of this article, you will understand What Exactly Async Main is in C# 7 and When and How to use Async Main with Examples.
Async Main is in C# 7:
There are two types of programs that the C# language compiler can build. One is a program with an entry point so that the Operating System can load the program and execute it from the entry point. The other is the program without an entry point. Operating Systems cannot directly execute such types of programs. Such type of programs needs to be referenced by other programs which have an entry point.
The Application which must have an entry point includes Windows Forms App, Console App, WPF App, ASP.NET and ASP.NET Core App, and Xamarian App. On the other hand, the Applications which do not have an entry point include the Class Library Projects.
Activating C# 7.1 features
To make Visual Studio 2017 use some other versions of C# follow the steps shown in the below image.
For those who need some more help, the steps are described here:
- Right-click on the project name and select Properties from the context menu
- Select the Build tab from the left
- Scroll down and click the Advanced button to open advanced build settings
- From the Language version drop-down select the C# version you are interested in
C# Latest Minor Version (latest) is the option to select if you want the latest version of C#.
Main Method in C#
Like other programming languages, the C# program also starts from the Main method with the following properties.
- The C# entry point method must be static,
- The name of the method must be the Main
- The return type of this method can be either void or int.
- It can have one parameter of a string array, containing any command-line arguments.
Before C# 7.1, there are four overloaded versions that are considered valid signatures for the Main method in C# as shown below.
public static void Main(); public static int Main(); public static void Main(string[] args); public static int Main(string[] args);
Before C# 7.1, when we wanted to call the async method from within the Main() method, then we need to add some code but now, the C# compiler does it for us. Let’s try to understand how to call the async method from Main in C# before C# 7.1 with an example. Please have a look at the below code for a better understanding. The following example code is self-explained, so please go through the comment lines for a better understanding.
using System; using System.Threading.Tasks; namespace AsyncMainDemo { class Program { static void Main(string[] args) { Console.WriteLine("Before C# 7.1, To use async method"); Console.WriteLine($"Main Method execution started at {System.DateTime.Now}"); //Calling Async Method //We cannot use await as the Main method is not async //Hence using Wait Method to wait for the completion of the SomeAsyncMethod() SomeAsyncMethod().Wait(); Console.WriteLine($"Main Method execution ended at {System.DateTime.Now}"); Console.WriteLine("Press any key to exist."); Console.ReadKey(); } //Async Method static async Task SomeAsyncMethod() { await Task.Delay(2000); } } }
Output:
Async Main in C# 7:
From C# 7.1, the Main() method which is the entry point of the application can be declared as async. Before C# 7.1, the Main() method can have a return type as either void or int; however, now, it also supports Task and Task<int>. So From C# 7.1, now we have eight overload versions that are considered as the valid signatures for the Main() method as shown below.
public static void Main(); public static int Main(); public static void Main(string[] args); public static int Main(string[] args); public static Task Main(); public static Task<int> Main(); public static Task Main(string[] args); public static Task<int> Main(string[] args);
Example to Understand Async Main Method in C# 7
Please have a look at the below example for a better understanding of the Async Main Method. In the below example, we have declared the Main method as async and Task as the return type. Then we call the SomeAsyncMethod using the await operator as now the Main method is an async method.
using System; using System.Threading.Tasks; namespace AsyncMainDemo { class Program { static async Task Main(string[] args) { Console.WriteLine("From C# 7.1, Async Main Method"); Console.WriteLine($"Main Method execution started at {System.DateTime.Now}"); //Calling Async Method using await as now the Main method is async await SomeAsyncMethod(); Console.WriteLine($"Main Method execution ended at {System.DateTime.Now}"); Console.ReadKey(); } //Async Method static async Task SomeAsyncMethod() { await Task.Delay(2000); } } }
Output:
As you can see in the above example, the Task.Delay is adding 2 seconds delay in the program execution. Now, C# 7.1 syntax is simpler and easy to use. In the above example, you can see how we use the Task within the Main method.
Example to Understand Async Main Method in C# 7 with Task<int>
Now, let’s take another example where we will see the use of Task<int>. Here, we will call another async method AdditionAsync from the Main method.
using System; using System.Threading.Tasks; namespace AsyncMainDemo { class Program { public static async Task<int> Main(string[] args) { Console.Title = "async Task<int> Main"; int number1 = 5, number2 = 10; Console.WriteLine($"Sum of {number1} and {number2} is: {await AdditionAsync(number1, number2)}"); Console.WriteLine("Press any key to exist."); Console.ReadKey(); return 0; } private static async Task<int> AdditionAsync(int no1, int no2) { await Task.Delay(2000); return no1 + no2; } } }
Output:
In the next article, I am going to discuss Exception Handling in C# with Examples. Here, in this article, I try to explain Async Main Method in C# 7 with Examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this Async Main Method in C# 7 article.
In the last section of this article, you have written AdditionAsync(int no1, int no2) using Task, I think since it’s is an immediately available operation, should it be written ValueTask as below
private static ValueTask AdditionAsync(int no1, int no2)
{
return Task.Run(() => SUM(no1, no2));
//Local function
int SUM(int x, int y)
{
return x + y;
}
}
Please suggest. Thanks!
“Press any key to exist” 😮