C# 仅通过构造函数设置类的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2357444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set properties of a class only through constructor
提问by Srikrishna Sallam
I am trying to make the properties of class which can only be set through the constructor of the same class.
我正在尝试制作只能通过同一类的构造函数设置的类的属性。
采纳答案by David Morton
Make the properties have readonly backing fields:
使属性具有只读支持字段:
public class Thing
{
private readonly string _value;
public Thing(string value)
{
_value = value;
}
public string Value { get { return _value; } }
}
回答by stephenbayer
This page from Microsoftdescribes how to achieve setting a property only from the constructor.
Microsoft 的此页面描述了如何仅从构造函数中实现设置属性。
You can make an immutable property in two ways. You can declare the set accessor.to be private. The property is only settable within the type, but it is immutable to consumers. You can instead declare only the get accessor, which makes the property immutable everywhere except in the type's constructor.
您可以通过两种方式创建不可变属性。您可以将 set accessor.to 声明为私有。该属性只能在类型内设置,但对消费者来说是不可变的。您可以改为仅声明 get 访问器,这使得该属性在除类型的构造函数之外的任何地方都是不可变的。
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.
在 Visual Studio 2015 随附的 C# 6.0 中,进行了一项更改,允许从构造函数设置仅获取属性。并且仅来自构造函数。
The code could therefore be simplified to just a get only property:
因此,可以将代码简化为仅获取属性:
public class Thing
{
public Thing(string value)
{
Value = value;
}
public string Value { get; }
}
回答by Dryadwoods
The correct way is:
正确的方法是:
public string MyProperty{ get; private set; }
public MyClassConstructor(string myProperty)
{
MyProperty= myProperty;
}
回答by Marc Ziss
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
从 c# 6.0 开始,您现在只能获取可以在构造函数中设置的属性(即使属性本身没有定义集合。请参阅带有私有 setter 的属性与 get-only-property
回答by Dhanuka777
With C# 9 it introduces 'init' keyword which is a variation of 'set', which is the best way to do this.
在 C# 9 中,它引入了 'init' 关键字,它是 'set' 的变体,这是执行此操作的最佳方法。
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 during object initialization:
今天的一大限制是属性必须是可变的,对象初始值设定项才能工作:它们通过首先调用对象的构造函数(在本例中为默认的无参数构造函数)然后分配给属性设置器来起作用。
仅初始化属性解决了这个问题!他们引入了一个 init 访问器,它是 set 访问器的变体,只能在对象初始化期间调用:
public class Person
{
public string FirstName { get; init; }
public string LastName { get; init; }
}
Ref: https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/