Get-Set 访问器功能因 get-set 关键字的存在而异
我目前正在实现 RSA 算法的穷人版本,我希望素数 d、e、m 和 n 是只读的,因为它们将在构造函数体内自动生成。但是,当我键入时,我得到两个不同的结果:
class RSA
{
public RSA()
{
n = 4;
}
private long n { get; private set; }
}
或
class RSA
{
public RSA()
{
n = 4;
}
private long n { get; }
}
阅读《Accelerated C#》一书,我的印象是可以使用自动实现的属性来实现私有集函数。事实证明我也可以在构造函数本身中做到这一点,但仅限于第一个版本。
阅读 C# 3.0 标准,它说:
同时具有 get 访问器和 set 访问器的属性是读写属性,仅具有 get 访问器的属性是只读属性,而具有 get 访问器的属性是只读属性。只有 set 访问器是只写属性。
但它们的行为并不相同。
简单的问题:为什么当我显式声明 private set
时可以初始化构造函数中的值,但如果我隐式声明则不能?这里有什么区别?
I'm currently implementing a poor-man's version of the RSA Algorithm and I wanted the prime numbers d, e, m, and n to be read-only as they will be automatically generated within ithe constructor body. However, I get two different results when I type:
class RSA
{
public RSA()
{
n = 4;
}
private long n { get; private set; }
}
or
class RSA
{
public RSA()
{
n = 4;
}
private long n { get; }
}
Reading the book Accelarated C#, I got the impression that a private set function can be implemented with auto-implemented properties. Turns out I can do it in the constructor itself too, but only for the first version.
Reading the C# 3.0 standard it says:
A property that has both a get accessor and a set accessor is a read-write property, a property that has only a get accessor is a read-only property, and a property that has only a set accessor is a write-only property.
Yet they don't behave equally.
Simple question: Why can I initialize the value in my constructor when I explicitly declare private set
, but not if I do it implicitly? What are the differences here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在第一种情况下,setter 是私有的。这意味着您可以在此类内部的任何位置设置此属性的值,而不仅仅是在构造函数中。在第二个示例中,没有设置器无法设置该值。使用 readonly 字段在语义上可能是最正确的您的情况,因为它将允许您在构造函数中或直接在声明字段时设置其值(如果该字段不应该从此类外部可见):
In the first case the setter is private. This means that you can set the value of this property everywhere from inside this class not only in the constructor. In the second example there's no setter won't be able to set the value. Using readonly field is probably most correct semantically in your case as it will allow you to set its value either in the constructor or directly when declaring the field (if this field shouldn't be visible from outside of this class):
不存在隐式自动生成的私有设置器之类的东西。如果省略
set;
,则无法为其分配值,该值将无法编译,因为它永远无法分配。此外,通过将
n
设置为私有,您无法从该类外部访问它。如果这是您的意图,那么使用财产根本没有意义。您可以简单地将其声明为字段:private readonly long n;
There's no such thing as an implicit autogenerated private setter. If you omit
set;
, you cannot assign it a value, which will not compile because it can never be assigned.Additionally, by making
n
private you cannot access it from outside that class. If that is your intention, there's no point in using a property at all. You can simply declare it as a field:private readonly long n;
如果您确实想要一个具有只读值的属性,您可以使用带有显式支持字段 IE 的属性:
If you really want a property with a readonly value, you could use a Property with the explicit backing field IE: