Can't specify the 'async' modifier on the 'Main' method of a console app

后端 未结 16 2424
后悔当初
后悔当初 2020-11-21 07:44

I am new to asynchronous programming with the async modifier. I am trying to figure out how to make sure that my Main method of a console applicati

16条回答
  •  醉梦人生
    2020-11-21 08:35

    In C# 7.1 you will be able to do a proper async Main. The appropriate signatures for Main method has been extended to:

    public static Task Main();
    public static Task Main();
    public static Task Main(string[] args);
    public static Task Main(string[] args);
    

    For e.g. you could be doing:

    static async Task Main(string[] args)
    {
        Bootstrapper bs = new Bootstrapper();
        var list = await bs.GetList();
    }
    

    At compile time, the async entry point method will be translated to call GetAwaitor().GetResult().

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

    EDIT:

    To enable C# 7.1 language features, you need to right-click on the project and click "Properties" then go to the "Build" tab. There, click the advanced button at the bottom:

    From the language version drop-down menu, select "7.1" (or any higher value):

    The default is "latest major version" which would evaluate (at the time of this writing) to C# 7.0, which does not support async main in console apps.

提交回复
热议问题