I am trying to make the properties of class which can only be set through the constructor of the same class.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Make the properties have readonly backing fields:
As of c# 6.0 you now can have get only properties that can be set in the constructor (even though there is no set defined in the property itself. See Property with private setter versus get-only-property
This page from Microsoft describes how to achieve setting a property only from the constructor.
In C# 6.0 included with Visual Studio 2015, there has been a change that allows setting of get only properties from the constructor. And only from the constructor.
The code could therefore be simplified to just a get only property:
The correct way is: