(C#) iterate over read-only private collection member

Posted by DGH on Stack Overflow See other posts from Stack Overflow or by DGH
Published on 2010-04-05T19:46:38Z Indexed on 2010/04/05 19:53 UTC
Read the original article Hit count: 574

Filed under:
|
|
|

I have a class which has two HashSet collections as private members. Other classes in my code would like to be able to iterate over those HashSets and read their contents. I don't want to write a standard getter because another class could still do something like myClass.getHashSet().Clear(); Is there any other way to expose the elements of my HashSets to iteration without exposing the reference to the HashSet itself? I'd love to be able to do this in a way that is compatible with for-each loops.

© Stack Overflow or respective owner

Related posts about c#

Related posts about hashset