Prevent other classes from altering a list in a class

后端 未结 4 1466
深忆病人
深忆病人 2020-12-06 05:53

If I have a class that contains, for example, a List and I want other classes to be able to see the list but not set it, I can declare



        
4条回答
  •  独厮守ぢ
    2020-12-06 06:46

    public class SomeClass()
    {
        private List _someList = new List();
    
        public IList SomeList 
        { 
             get { return _someList.AsReadOnly(); } 
        }
    }
    

提交回复
热议问题