Prevent other classes from altering a list in a class

后端 未结 4 1467
深忆病人
深忆病人 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:33

    You won't be able to use an autoproperty.

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

提交回复
热议问题