If I have the following property with no setter or backing field
public List<string> TestProperty => new List<string> {"one", "two"};
and try to initialise it in an object initialiser using
TestProperty = new List<string> {"three", "four"}
I get the expected error:
[CS0200] Property or indexer 'X.TestProperty' cannot be assigned to -- it is read only
However if I try to initialise it in an object initialiser using
TestProperty = { "three", "four" }
everything compiles fine but the property still returns new List<string> {"one", "two"};
.
@jon-skeet explained in this answer that this second form is a nested object initialiser but I'm still not sure why the above (unhelpfully) compiles and whether this can be stopped and the property removed from IDE auto-complete (ReSharper) in the object initialiser? This can only frustrate users of my code.
Edit: note that I do not want to initialise this property, my concern is that the complier allows something that looks like an initialisation to occur.