10

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

11
  • 1
    C# 9.0 require .Net 5 and to run it you need Visual Studio 2019 Version 16.8 (source). Typo?
    – Sinatr
    Commented Nov 11, 2020 at 15:14
  • No repro. C# 9 is used by default with .NET 5 RTM, in VS 16.8 (and most of the previews). What VS version and SDK version are you using? Commented Nov 11, 2020 at 15:16
  • 1
    BTW post the code, not images of the code. Images can't be copied and compiled. As for using string? you do understand that if you store a null there you won't be able to replace it? Are you trying to use Person with an ORM perhaps? Commented Nov 11, 2020 at 15:19
  • 1
    For older frameworks it was <TargetFrameworkVersion>, was it changed? Another stupid question: is it C# project?
    – Sinatr
    Commented Nov 11, 2020 at 15:31
  • 1
    So I'm coming to conclusion that it's probably a bug in Visual Studio...
    – El Mac
    Commented Nov 11, 2020 at 15:46

3 Answers 3

22

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.

2
  • In Visual Studio, there was an Error unexpected token. I don't remember if it also appeared in the Build Errors. When I built the application using the dotnet CLI, it built (and ran) successfully, which proved there is no syntax error.
    – El Mac
    Commented Nov 11, 2020 at 16:34
  • 2
    Thanks for your update. I was having the same issue and hadn't thought of Resharper as the cause. Who'd have thought they wouldn't support c# 9 at its release? Anyhow, I moved to the Early Access Program (#7) and the problem is resolved. Commented Nov 14, 2020 at 5:51
4

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

1

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

5
  • I did, it compiles otherwise
    – Peter Bons
    Commented Nov 11, 2020 at 15:15
  • @ElMac but that's the answer. If you target .NET 5, you get C# 9 by default. If you have issues, you may be using the wrong VS version and/or using a pre-release version of .NET 5 Commented Nov 11, 2020 at 15:15
  • @PeterBons The Target Framework is set in my CSPROJ File. I believe it's set correctly?
    – El Mac
    Commented Nov 11, 2020 at 15:35
  • @PeterBons I haven't updated the ASP.NET Core dependencies to 5.0 yet, but I believe that should be no issue - right?
    – El Mac
    Commented Nov 11, 2020 at 15:36
  • @ElMac the framework dependencies like Microsoft.AspNetCore.App and \Microsoft.NETCore.App should refer to .Net 5.
    – Peter Bons
    Commented Nov 11, 2020 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.