What is the difference between a property with a omitted setter and a property with a private setter?
public string Foo { get; private set; }
vs
public string Foo { get; }
Private set intersection is a secure multiparty computation cryptographic technique that allows two parties holding sets to compare encrypted versions of these sets in order to compute the intersection. In this scenario, neither party reveals anything to the counterparty except for the elements in the intersection.
Use private set when you want setter can't be accessed from outside. Use readonly when you want to set the property only once. In the constructor or variable initializer.
Using only private(set) means that the getter is internal - so not accessible outside the module.
Since it is a read operation on a field, a getter must always return the result. A public getter means that the property is readable by everyone. While the private getter implies that the property can only be read by class and hence is a write-only property.
In C# 6, get;
only properties are only settable from the constructor. From everywhere else, it is read-only.
A property with a private set;
can be set from everywhere inside that class.
From outside the class, it won't change anything if you use this syntax:
public string Foo { get; }
But you won't be able to update Foo
within the class, except in the constructor, to do so, you'll need the private setter:
public string Foo { get; private set; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With