dotnet / dotnet/dotnet-api-docs

Example uses feature only available in C# 7.1

Open
#1,056 4 comments 0 reactions 0 assignees View on GitHub
area-System.IO doc-enhancement dotnet-api/prod help wanted Pri2
Dominant language
C#
Stars
949
Forks
1.7k
Avg merge
3d 27m
Merged PRs (30d)
49

Description

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.*

* ID: aa3850da-1adf-a7e9-4c64-5d8327839798
* Version Independent ID: 167ba859-4b1e-eaa4-8d9a-c2e695583187
* Content: [StreamReader.ReadLineAsync Method (System.IO)](https://docs.microsoft.com/en-us/dotnet/api/system.io.streamreader.readlineasync?view=netframework-4.7.2)
* Content Source: [xml/System.IO/StreamReader.xml](https://github.com/dotnet/dotnet-api-docs/blob/master/xml/System.IO/StreamReader.xml)
* Product: **dotnet-api**
* GitHub Login: @dotnet-bot
* Microsoft Alias: **dotnetcontent**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.