Using C# 7.1 Features

With the release of Visual Studio 2017 update 3, the new C# 7.1 features became available.

To use the new features, the .csproj file can be modified and the <LangVersion> element set to either “latest” (the newest release including minor releases) or explicitly to “7.1” , for example:

<LangVersion>latest</LangVersion>

or

<LangVersion>7.1</LangVersion>

To select one of these in Visual Studio,  go to the project properties and the build tab and choose the “Advanced…” button as the following screenshot shows:

Visual Studio 2017 Screenshot showing C# 7.1 enabled

Now the new features of C# 7.1 including  asynchronous main methods are available.

C# 7.1 Async Main Methods

With C# 7.0, in a console app the entry point could not be marked async requiring some workarounds/boilerplate code, for example:

class Program
{
    static void Main(string[] args)
    {
       MainAsync().GetAwaiter().GetResult();
    }

    private static async Task MainAsync()
    {
        using (StreamWriter writer = File.CreateText(@"c:\temp\anewfile.txt"))
        {
            await writer.WriteLineAsync("Hello");
        }
    }
}

With C# 7.1, the main method can be async, as the following code shows:

class Program
{
    static async Task Main(string[] args)
    {
        using (StreamWriter writer = File.CreateText(@"c:\temp\anewfile.txt"))
        {
            await writer.WriteLineAsync("Hello");
        }
    }
}

You can check out the C#7.1 features on GitHub such as:

Comments (5) -

  • Julien Couvreur

    8/24/2017 8:54:42 AM | Reply

    Note: C# 7.1 has shipped (with Visual Studio 2017 version 15.3). The features are no longer candidates.
    github.com/.../Language-Version-History.md

    • Jason Roberts

      8/25/2017 4:57:13 AM | Reply

      Hi Julien, I've edited the hyperlink text to avoid any confusion - thanks Smile

  • Howard

    8/25/2017 12:57:35 PM | Reply

    Anyone know if/when C# 7.1 will be supported on build agents in VSTS?
    I can create a VS2017 app with c# 7.1 but it fails to build on the agents.

    • Julien Couvreur

      8/28/2017 4:58:58 AM | Reply

      Howard,
      I'm not very familiar with the VSTS scenario. Would you mind filing an issue on the Roslyn repo and tagging me (@jcouv)? I'll  get in touch with VSTS team.
      https://github.com/dotnet/roslyn/issues/new
      Thanks

      • Howard

        8/31/2017 11:29:39 AM | Reply

        Just in case anyone else stumbles across this issue as well, the solution was pretty simple.

        I'd put a C# 7.1 specification in the project under the *debug* settings. But the Build definition the Agent ran was the *release* configuration, which didn't have the 7.1 setting. Check that first!!

Add comment

Loading