2

Here I have two functions static int Main() and static async Task<int> Main().

Can anyone tell me what is the difference between them?

static int Main()
{
    return DoAsyncWork().GetAwaiter().GetResult();
}

static async Task<int> Main()
{
    return await DoAsyncWork();
}

and is await.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Minhaj Patel
  • 579
  • 1
  • 6
  • 21

5 Answers5

7

The difference is small and purely cosmetic.

While I couldn't find a sample of the code that calls main the specification states that this feature just removes 'boiler plate code'.

So the implementation is something like

static int $Main()  // compiler generated
{
    return Main().GetAwaiter().GetResult();
}

static async Task<int> Main()
{
    return await DoAsyncWork();
}
H H
  • 263,252
  • 30
  • 330
  • 514
3

Async Main is actually not the method that runs first. Compiler generates standard Main method that calls the async version of Main:

static async Task Main(string[] args)
{
    await Task.Delay(1);

    Console.ReadLine();
}

// This gets generated by the compiler.
static void Main(string[] args)
{
    Main(args).GetAwaiter.GetResult();
}

So the difference is that when using async Main, there are two methods called Main. One async and one generated by the compiler.

Because these two methods have the same name and parameters and only differ by return type, compiler generates method with name <Main>, not Main. This is the generated signature:

.method private hidebysig specialname static 
        void '<Main>' (
            string[] args
        ) cil managed 
FCin
  • 3,804
  • 4
  • 20
  • 49
2

Main method is main entry point of Program in C#. Main method usually comes in following flavors (overloads):

public static void Main(string[] args);
public static int Main(string[] args);
public static void Main();
public static int Main();

The 'int' ones are ones generally used in native world where the return value needs to be evaluated.

Now, coming to the 'async main'. Following is invalid:

public static async Task Main(string[] args) 

and will throw a compile time error:

Program does not contain a static 'Main' method suitable for an entry point

A workaround is either to call Wait or async wait on an operation. Either:

public static void Main(string[] args)
{     
    BuildWebHost(args).RunAsync().GetAwaiter().GetResult();
}

Or:

public static void Main(string[] args)
{
    BuildWebHost(args).RunAsync().Wait();
}

Personally, I feel async main is more of a candy. Async main or Main with async just makes asynchronous operations easy to run from the main entry of the program.

Reference here: https://blogs.msdn.microsoft.com/mazhou/2017/05/30/c-7-series-part-2-async-main/

Gauravsa
  • 6,330
  • 2
  • 21
  • 30
0

async avoids waiting in main thread on long running operations.Using async mechanism, we can just trigger long running operations and continue with other tasks.

 public async Task<int> ExampleMethodAsync()  
 {  
   await httpClient.GetStringAsync(requestUrl); 
 }  

The Main method is the entry point of a C# application. (Libraries and services do not require a Main method as an entry point.) When the application is started, the Main method is the first method that is invoked.

static void Main(string[] args)
{
    // Display the number of command line arguments:
    System.Console.WriteLine(args.Length);
}
Kamrul Hasan
  • 127
  • 1
  • 12
0

static int main() is a synchronous method ,meaning that your thread must do nothing else from the point where you request the work to be done until the point where its done.

static async Task main() is a asynchronous method, meaning your thread tells the method that it needs the work to be done, and the method will complete the work and tell you when its completed