Forbid public Add and Delete for a List

前端 未结 8 1783
被撕碎了的回忆
被撕碎了的回忆 2021-02-19 02:11

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

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


        
8条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 02:58

    Beside of implementing your own IList you could return a ReadOnlyCollection.

    public MyClass
    {
        public MyClass Parent;
    
        private List children;
        public ReadOnlyCollection Children
        {
            get { return children.AsReadOnly(); }
        }
    }
    

提交回复
热议问题