The. NET Core Console application uses the asynchronous (Async) Main method

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.