Enabling C# 9 in .NET 5 Preview 5 raises a compiler error for the new C# 9 feature

Viewed 7416

I followed this answer: How to enable C# 9.0-preview to enable C# 9.

I installed .NET 5 preview 5 which includes the new C# 9.

Microsoft.NETCore.App 5.0.0-preview.5.20278.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

I try to use C# 9:

public class Person
{
    public string FirstName { get; init; }
    public string LastName { get; init; }
}

with a project setting like:

<LangVersion>9</LangVersion>

but I get a compilation error:

Error CS1617 Invalid option '9' for /langversion. Use '/langversion:?' to list supported values.

I didn't find 9 in the list when run:

csc -langversion:?

The list is:

default
1
2
3
4
5
6
7.0
7.1
7.2
7.3
8.0 (default)
latestmajor
preview
latest

Then I used as suggested by this answer, but it can't help and also this.

<LangVersion>preview</LangVersion>

But I get a compilation error.

Program.cs(26,40): error CS1014: A get or set accessor expected

What did I miss to do to use C# 9 in .NET 5 preview 5?

2 Answers

I enabled C# 9 as described in this answer and @PanagiotisKanavos:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net5.0</TargetFramework>
  <LangVersion>Preview</LangVersion>
</PropertyGroup>

.NET 5 Preview 5 or Preview 6 don't support the init feature and majority of C# 9.

I installed latest SDK bits of development from https://aka.ms/dotnet/net5/dev/Sdk/dotnet-sdk-win-x64.exe.

Currently preview 8 is available (version 5.0.100-preview.8.20327.5).

I can build projects including init feature in Visual Studio 2019.6.2 (the editor still show red lines, but build success) or using Visual Studio Code (perfect, and IDE sense the new C# 9 syntax).

Most, almost all, features of C# 9 in the blog post are working fine with .NET 5 Preview 8.

Update July 3, 2020

Alternative Solution

For working with .NET 5 Preview 5, install the package Microsoft.Net.Compilers.Toolset, Version 3.8.0-1.20330.5. It support init feature and many c# 9 new features.

Install-Package Microsoft.Net.Compilers.Toolset -Version 3.8.0-1.20330.5 -Source https://dotnet.myget.org/F/roslyn/api/v3/index.json

Update Aug 7, 2020

Visual Studio 2019 version 16.7 is released with support C# 9.

In developer Command Prompt, type the command:

csc -langversion:?

Supported language versions:
default
1
2
3
4
5
6
7.0
7.1
7.2
7.3
8.0 (default)
9.0
latestmajor
preview
latest

You see 9.0 in the list above, and you can define LangVersion as 9.0:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net5.0</TargetFramework>
  <LangVersion>9.0</LangVersion>
</PropertyGroup>

You need not to install Microsoft.Net.Compilers.Toolset v 3.8.0-1.20330.5 because C# 9 is supported.

Following Getting Setup With C# 9 Preview:

There are two main things specific to Visual Studio.

  • Ensure that Visual Studio is updated to the very latest version. I cannot tell you how many times things don't work in Visual Studio, but do work from the command line or Visual Studio Code, and it's because of the version of Visual Studio.

  • Secondly, you need to go menu ToolsOptions → "Preview Features" and then tick the box that says "Use previews of the .NET Core SDK". I think they added this feature so that you could "play" with the preview SDK's, but not have your everyday work being built by preview features. But you need it ticked for things to work.

Enter image description here

Related