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

你离开我真会死。 提交于 2020-12-12 05:38:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!