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?