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

后端 未结 16 2434
后悔当初
后悔当初 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:38

    C# 7.1 (using vs 2017 update 3) introduces async main

    You can write:

       static async Task Main(string[] args)
      {
        await ...
      }
    

    For more details C# 7 Series, Part 2: Async Main

    Update:

    You may get a compilation error:

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

    This error is due to that vs2017.3 is configured by default as c#7.0 not c#7.1.

    You should explicitly modify the setting of your project to set c#7.1 features.

    You can set c#7.1 by two methods:

    Method 1: Using the project settings window:

    • Open the settings of your project
    • Select the Build tab
    • Click the Advanced button
    • Select the version you want As shown in the following figure:

    Method2: Modify PropertyGroup of .csproj manually

    Add this property:

        7.1
    

    example:

        
            AnyCPU
            true
            full
            false
            bin\Debug\
            DEBUG;TRACE
            prompt
            4
            false
            7.1
            
    

提交回复
热议问题