If I have a class that contains, for example, a List<string> and I want other classes to be able to see the list but not set it, I can declare
public class SomeClass()
{
public List<string> SomeList { get; }
}
This will allow another class to access SomeList and not set it.
However, although the calling class can't set the list, it can add or remove elements. How do I prevent that? I guess I could use a field and return a copy of the List instead of using a property, but that just doesn't feel right.
(This should be very simple but I must be missing something....)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…