understanding private setters
Solution 1:
Logically.
The presence of a private setter is because you can use auto property:
public int MyProperty { get; set; }
What would you do if you want to make it readonly?
public int MyProperty { get; }
Oh crap!! I can't access it from my own class; I should create it like a normal property:
private int myProperty;
public int MyProperty { get { return myProperty; } }
Hmm... but I lost the "Auto Property" feature...
public int MyProperty { get; private set; }
AHHH.. that is better!!
Solution 2:
A private setter is useful if you have a read only property and don't want to explicitly declare the backing variable.
So:
public int MyProperty
{
get; private set;
}
is the same as:
private int myProperty;
public int MyProperty
{
get { return myProperty; }
}
For non auto implemented properties it gives you a consistent way of setting the property from within your class so that if you need validation etc. you only have it one place.
To answer your final question the MSDN has this to say on private setters:
However, for small classes or structs that just encapsulate a set of values (data) and have little or no behaviors, it is recommended to make the objects immutable by declaring the set accessor as private.
From the MSDN page on Auto Implemented properties
Solution 3:
With the introduction of C# 6.0 and the syntax for Auto-Property Initializers, private setters are no longer needed for properties that are only set upon initialization, either inline or within the constructor.
These new syntaxes now compile:
Inline initialized property
public class MyClass1 {
public string MyProperty { get; } = "Aloha!"
}
Constructor initialized property
public class MyClass2 {
public string MyProperty { get; }
public MyClass2(string myProperty) {
MyProperty = myProperty;
}
}
Solution 4:
It's rather simple. Private setters allow you to create read-only public or protected properties.
That's it. That's the only reason.
Yes, you can create a read-only property by only specifying the getter, but with auto-implmeneted properties you are required to specify both get and set, so if you want an auto-implemented property to be read-only, you must use private setters. There is no other way to do it.
It's true that Private setters were not created specificlly for auto-implemented read-only properties, but their use is a bit more esoteric for other reasons, largely centering around read-only properties and the use of reflection and serialization.
Solution 5:
I don't understand the need of having private setters which started with C# 2.
For example, the invoice class allows the user to add or remove items from the Items property but it does not allow the user from changing the Items reference (ie, the user cannot assign Items property to another item list object instance).
public class Item
{
public string item_code;
public int qty;
public Item(string i, int q)
{
this.item_code = i;
this.qty = q;
}
}
public class Invoice
{
public List Items { get; private set; }
public Invoice()
{
this.Items = new List();
}
}
public class TestInvoice
{
public void Test()
{
Invoice inv = new Invoice();
inv.Items.Add(new Item("apple", 10));
List my_items = new List();
my_items.Add(new Item("apple", 10));
inv.Items = my_items; // compilation error here.
}
}