问题
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?
回答1:
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
回答2:
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);
}
来源:https://stackoverflow.com/questions/64821207/some-c-sharp-9-features-not-available-after-upgrading-asp-net-core-3-1-app-to-n