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; }
}
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
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.
string?
you do understand that if you store anull
there you won't be able to replace it? Are you trying to usePerson
with an ORM perhaps?<TargetFrameworkVersion>
, was it changed? Another stupid question: is it C# project?