I have a public property of type List<>
in class MyClass
. It is public because code outside of MyClass
(including the XAML bindings) need to be able to access and change the data. However, the one thing that elements outside of MyClass
should NOT be able to do is call the Add()
method. The only way to add a new method to the list should be through the MyClass.MyListAdder()
method.
Is there a way to accomplish this?
You can try switching to ReadOnlyCollection<T>
. It has slightly different API than List<T>
but might be good enough for your needs.