다음을 통해 공유


Async Main() is available, but hidden

How did I miss this?

I love C# and I try to keep up with the latest features. What I didn’t realize until recently is that the C# 7.1 features are already released. And C# 7.1 includes asynchronous Main methods. Awesome!

By default, new projects in Visual Studio only work with the latest major version of the language (which would be 7.0), but you can change that. So, if you try something like this

capture20171208161849214

you will get this compiler error because Main methods are not allowed to be async (until C# 7.1)

“error CS8107: Feature 'async main' is not available in C# 7. Please use language version 7.1 or greater.”

But… open the properties page for your project and click the Advanced button on the Build tab (you might have to scroll down to see it)

capture20171208162336206

and you’ll see the setting to select which compiler version to use. Change “language version” from “latest major version” to “latest minor version”

capture20171208162548723

If you have Visual Studio 2017 v15.3 or later, you the latest version will be C# 7.1. You will now be able to compile and run code with asynchronous Main methods. Plus you will get all the other goodies that have been added to C# recently.

If you are interested in C# language features, you can track their progress at the C# language design repo on GitHub

The sample code above is available as a gist.

Comments

  • Anonymous
    December 11, 2017
    Hi,You can also set the language version by using the light bulb or by changing the csproj manually:https://www.meziantou.net/2017/08/24/3-ways-to-enable-the-latest-c-features
  • Anonymous
    February 14, 2019
    Need to bear in mind that the default is "C# latest major version" but you can't rely on that being the real latest, for example, my system has 7.0, 7.1, 7.2, 7.3 and 8.0 (beta), but "latest" chooses 7.0. I get why it wouldn't choose a beta 8.0, but surely "latest" should see 7.3 as later than 7.0