开发者

Best practice to create typed collections/lists with internal or private Add, Remove functionality?

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜