In versions of C# prior to 7.0, a Main
function could not be declared as async
, therefore restricting the use of await
inside the Main
function. As a result, if an async
function had to be called inside the Main
function, then a GetAwaiter
function call had to be made in order to process the result asynchronously:
static int Main() {
return DoAsyncTask().GetAwaiter().GetResult();
}
However, in C# 7.0 onwards, the Main
function can be declared as async
, therefore allowing the use of await
inside it.
The following code provides an example of how to use the async
Main
method:
class Program {static async Task<int> Main(){return await DoAsyncTask();}}
In the example above, the Main
method is declared as async
, and therefore we can use await
in line 4. The return type of the Main
method has to be of the type Task<int>
if an exit code is to be returned, or Task
if no exit code is to be returned.
Free Resources