C # 7.1 and later allows us to use the Async Main method.
I. Create a new Console application
Two. Async Main method
We will directly change the main method to read as follows:
static async Task Main(string[] args)
You can see the error, prompting us to be a C # 7.1 feature. We have two ways to solve, in fact, the end is the same, but the operation is not the same.
1. First method-Modify the Csproj file
Open the project's csproj file and add the following code:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <LangVersion>7.1</LangVersion> </PropertyGroup>
For example:
2. Second Method-Change project properties by vs
In the Solution Manager interface, select the project, and then right-click Properties, Build
In the popup screen, selectC# 7.1
After the above changes, no error will be made.
Three. Verification
In the main method, we add the following code to get the HTML of Baidu homepage:
class Program{ static async Task Main(string[] args) { var client = new HttpClient(); var result = await client.GetStringAsync("https://www.baidu.com/"); Console.WriteLine(result); Console.ReadKey(); }}
Then run:
The code used in this article: HTTPS://GITHUB.COM/STULZQ/BLOGDEMOS/TREE/MASTER/ASYNCCONSOLEAPP
The. NET Core Console application uses the asynchronous (Async) Main method