Forbid public Add and Delete for a List<T>

Posted by aaginor on Stack Overflow See other posts from Stack Overflow or by aaginor
Published on 2010-06-16T12:01:30Z Indexed on 2010/06/16 12:02 UTC
Read the original article Hit count: 193

Filed under:
|

Hi folks,

in my C#-project, I have a class which contains a List

public class MyClass
{
  public MyClass parent;
  public List<MyClass> children;
  ...
}

I want to prevent the user of the class from Adding (and Deleting) an Element to the children-List, but he shall still be able to parse its elements. I want to handle the Adding and Deleting within MyClass, providing an AddChild(MyClass item) and DeleteChild(MyClass item) to ensure that, when an item is added to the child list, the parent of that item will be set properly.

Any idea how to do this besides implementing my own IList?

Thanks in advance, Frank

© Stack Overflow or respective owner

Related posts about c#

Related posts about list