Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Some C# 9 features not available after upgrading Asp.Net Core 3.1 app to .Net 5

I've upgraded an Asp.Net Core 3.1 (MVC) to .Net 5 by modifying the corresponding *.csproj file to this:

<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>

Now I can use the C# 9 target typing feature...

string s = new('c', 3); // compiles fine

...but I can't create a record class:

public data class User
{
  // IDE1007 The name 'data' does not exist in the current context. 
}

Am I missing something here?

like image 262
Mats Avatar asked Oct 16 '25 18:10

Mats


2 Answers

According to record type specs, you should use public record User syntax.

It's better to look at final specs rather then blog post with introduction, since some things were changed.

You can also refer to csharplang repo in GitHub to check the most recent specs, design meetings and proposals. For particular Records feature the initial issue #39 might be used to track the most recent updates and specs

like image 135
Pavel Anikhouski Avatar answered Oct 18 '25 09:10

Pavel Anikhouski


The keyword for records types is record now:

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

    public Person(string first, string last) => (FirstName, LastName) = (first, last);
}
like image 34
Guru Stron Avatar answered Oct 18 '25 09:10

Guru Stron