Skip to content

Example uses feature only available in C# 7.1 #1056

@Camios

Description

@Camios

In the example, the entry point's signature "static async Task Main()" is only available for c# v7.1 and above.
Visual Studio defaults to the major version which at time of writing is only 7.0 and most users will get a compilation error, unless they modify the advanced build properties to change the c# version. Is this asking too much of someone trying to learn the framework?
For those using C# 7.0 the main method would look more like this:
static void Main()
{
ReadCharacters().Wait();
}

Note: there's appears to be conflicting opinions on using Wait() vs GetAwaitor().GetResult(). The documentation for GetAwaiter states it is for compiler use only, but I've seen discussions suggesting it should be used: https://stackoverflow.com/questions/17284517/is-task-result-the-same-as-getawaiter-getresult

BTW C# 7.1 and above use GetAwaitor().GetResult() - it generates code which replaces and wraps the async entry point with GetAwaitor().GetResult().
public static void Main(string[] args) => $MainEntrypoint$(args).GetAwaitor().GetResult();
private static async Task $MainEntrypoint$(string[] args) { await ... }
https://blogs.msdn.microsoft.com/mazhou/2017/05/30/c-7-series-part-2-async-main/


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pri2Indicates issues/PRs that are medium priorityarea-System.IOdoc-enhancementImprove the current contentdotnet-api/prodhelp wantedGood for community contributors to help [up-for-grabs]

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions