c#-9.0

What is difference between Init-Only and ReadOnly in C# 9?

丶灬走出姿态 提交于 2020-08-10 19:29:46
问题 I am going through C# 9 new features which will be released soon. Init-Only properties are being introduced with it. The one big limitation today is that the properties have to be mutable for object initializers to work: They function by first calling the object’s constructor (the default, parameterless one in this case) and then assigning to the property setters. Init-only properties fix that! They introduce an init accessor that is a variant of the set accessor which can only be called

How to enable C# 9.0-preview

本秂侑毒 提交于 2020-07-30 04:43:07
问题 I have downloaded and installed v5.0.0-preview.5 . My project is targeting net5.0 but C# 9.0 is not working. How can I enable C# 9.0 ? 回答1: According to this page in the documentation you need to edit your *.csproj to set the <LangVersion> to preview . Also mentioned in the blog-post about the preview-release, but not the above documentation page, is that you need to update your project's targetFramework property too to net5.0 (this is because the C# design team decided to restrict entire C#

with expression vs new keyword

倖福魔咒の 提交于 2020-06-29 06:45:27
问题 I was reading the devblogs about "what's new in C#9.0", then I noticed "with expression". public data class Person { public string FirstName { get; init; } public string LastName { get; init; } } var otherPerson = person with { LastName = "Hanselman" }; they say A record implicitly defines a protected “copy constructor” – a constructor that takes an existing record object and copies it field by field to the new one: protected Person(Person original) { /* copy all the fields */ } // generated

with expression vs new keyword

一个人想着一个人 提交于 2020-06-29 06:45:18
问题 I was reading the devblogs about "what's new in C#9.0", then I noticed "with expression". public data class Person { public string FirstName { get; init; } public string LastName { get; init; } } var otherPerson = person with { LastName = "Hanselman" }; they say A record implicitly defines a protected “copy constructor” – a constructor that takes an existing record object and copies it field by field to the new one: protected Person(Person original) { /* copy all the fields */ } // generated