El Mac
El Mac

Reputation: 3419

Support for C# 9.0 in Visual Studio

How can I turn on the support for the records keyword in C# 9.0?

When I try to use the new record keyword (or the init), I get an unexpected token error in Visual Studio.

What am I missing? Or is C# 9.0 not supported in Visual Studio 16.8?

public record Person
{
    public string? FirstName { get; init; }
    public string? LastName { get; init; }
}

Error Screenshot


Update: Just read the marked answer. The information below ended up not being important for this problem and is here just for documentation purposes.


I am using Visual Studio 16.8 with the language set to 9.0 in my project file.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <LangVersion>9.0</LangVersion>
    <TargetFramework>net5.0</TargetFramework>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
    <Nullable>enable</Nullable>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="3.1.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.9">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
  </ItemGroup>

</Project>

Visual Studio Version

Visual Studio Version

Installed SDKs

2.1.2 [C:\Program Files\dotnet\sdk]
2.1.104 [C:\Program Files\dotnet\sdk]
2.1.201 [C:\Program Files\dotnet\sdk]
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.301 [C:\Program Files\dotnet\sdk]
2.1.400 [C:\Program Files\dotnet\sdk]
2.1.508 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.1.801 [C:\Program Files\dotnet\sdk]
2.2.300 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]
3.1.201 [C:\Program Files\dotnet\sdk]
5.0.100 [C:\Program Files\dotnet\sdk]

Proof that it's running .NET 5.

So to make sure it's really running .NET 5, I have created a route to output the .NET runtime version. Looks like it's really running .NET 5.

enter image description here

Upvotes: 10

Views: 5894

Answers (3)

El Mac
El Mac

Reputation: 3419

After analyzing the loading times of the error message and receiving the same error message in the Rider IDE, I came to conclusion it had to be Resharper.

I uninstalled Resharper and the error messages disappeared.


Update

Resharper 2020.3 and Rider 2020.3 are released now and officially support .NET 5 and C# 9.

Upvotes: 22

Piers Myers
Piers Myers

Reputation: 10899

You need to use ReSharper 2020.3 for it to work with .NET 5 and C# 9.0.

Upvotes: 4

Peter Bons
Peter Bons

Reputation: 29730

Make sure to target the .Net 5 framework.

C# 9.0 is supported on .NET 5. For more information, see C# language versioning.

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9

Upvotes: 1

Related Questions