I frequently expose typed collections and lists. Often when exposing collections and lists I am not happy with the Add and Remove methods being public. In older versions of .Net I have implemented IE开发者_运维技巧numerable but this is a lot of work.
What are the better alternatives?
I have seen questions similar to this, but not specifically about the Add / Remove accessors
Thanks.
Does ReadOnlyCollection(T) sove this?
EDIT
Since you want...
...to be able to add to the collection / list internally or privately
you can have a private IList<T>
you'll be adding objects to, and the ReadOnlyCollection<T>
will be a wrapper around this list. Since it's a wrapper, all changes to an underlying list will be reflected in this read-only collection.
Expose interface:
IEnumerable<T>
精彩评论