I have an Item class that has a public NoSetter member that does not contain setter. The object explicitly indicates get, which retrieves the private Listedly List object.
class Item { private readonly List<string> emptyString; public Item() { this.emptyString = new List<string>(); } public List<string> NoSetter { get { return this.emptyString; } } }
When you create this object, you cannot set NoSetter to the list, the compiler does not work when you try.
List<string> goldenString = new List<string>(); goldenString.Add("Nope"); Item item = new Item() { NoSetter = goldenString
However, if you create an inline list, you can install NoSetter.
Item item = new Item() { NoSetter = { "But this still works" } };
I expect the NoSetter.Get method to return a readonly emptyString, but instead returns an inline NoSetter object. What causes this in .net? Expected?
source share