Forbid public Add and Delete for a List

前端 未结 8 1190
我在风中等你
我在风中等你 2021-02-19 02:12

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

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


        
相关标签:
8条回答
  • 2021-02-19 02:34

    Make the list private, and create public methods to access its content from outside the class.

    public class MyClass
    {
        public MyClass Parent { get; private set; }
    
        private List<MyClass> _children = new List<MyClass>();
        public IEnumerable<MyClass> Children
        {
            get
            {
                // Using an iterator so that client code can't access the underlying list
                foreach(var child in _children)
                {
                    yield return child;
                }
            }
        }
    
        public void AddChild(MyClass child)
        {
            child.Parent = this;
            _children.Add(child);
        }
    
        public void RemoveChild(MyClass child)
        {
            _children.Remove(child);
            child.Parent = null;
        }
    }
    

    By the way, avoid declaring public fields, because you can't control what client code does with them.

    0 讨论(0)
  • 2021-02-19 02:39

    Beside of implementing your own IList<T> you could return a ReadOnlyCollection<MyClass>.

    public MyClass
    {
        public MyClass Parent;
    
        private List<MyClass> children;
        public ReadOnlyCollection<MyClass> Children
        {
            get { return children.AsReadOnly(); }
        }
    }
    
    0 讨论(0)
  • 2021-02-19 02:40
    using System.Collections.ObjectModel;
    
    public class MyClass
    {
        private List<MyClass> children;
    
        public ReadOnlyCollection<MyClass> Children
        {
            get
            {
                return new ReadOnlyCollection<MyClass>(children);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-19 02:40

    You don't need to reimplemented IList, just encapsulate a List in a class, and provide your own Add and Delete functions, that will take care of adding and setting the required properties.

    You might have to create your own enumerator if you want to enumerate through the list though.

    0 讨论(0)
  • 2021-02-19 02:47

    You can encapsulate the list in the class by making it private, and offer it as a ReadOnlyCollection:

    public class MyClass {
    
      public MyClass Parent { get; private set; };
    
      private List<MyClass> _children;
    
      public ReadOnlyCollection<MyClass> Children {
        get { return _children.AsReadOnly(); }
      }
    
      public void AddChild(MyClass item) {
        item.Parent = this;
        _children.Add(item);
      }
    
      public void DeleteChild(MyClass item) {
        item.Parent = null;
        _children.Remove(item);
      }
    
    }
    

    You can make the Parent a property with a private setter, that way it can't be modified from the outside, but the AddChild and DeleteChild methods can change it.

    0 讨论(0)
  • 2021-02-19 02:53

    Expose the list as IEnumerable<MyClass>

    0 讨论(0)
提交回复
热议问题