我目前正在实现 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; }
}
阅读 Accelarated C# 一书,我得到的印象是可以使用自动实现的属性来实现私有集合函数。结果我也可以在构造函数本身中做到这一点,但仅限于第一个版本。
阅读 C# 3.0 标准,它说:
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.
然而,他们的行为并不相同。
简单的问题:为什么当我显式声明时可以初始化构造函数中的值private set
,但如果我隐式执行则不能?这里有什么区别?