An entry point cannot be marked with the 'async' modifier

前端 未结 5 1807
甜味超标
甜味超标 2020-12-05 13:08

I copied below code from this link.But when I am compiling this code I am getting an entry point cannot be marked with the \'async\' modifier. How can I m

5条回答
  •  攒了一身酷
    2020-12-05 13:36

    The error message is exactly right: the Main() method cannot be async, because when Main() returns, the application usually ends.

    If you want to make a console application that uses async, a simple solution is to create an async version of Main() and synchronously Wait() on that from the real Main():

    static void Main()
    {
        MainAsync().Wait();
    }
    
    static async Task MainAsync()
    {
        // your async code here
    }
    

    This is one of the rare cases where mixing await and Wait() is a good idea, you shouldn't usually do that.

    Update: Async Main is supported in C# 7.1.

提交回复
热议问题