How to handle add to list event?

后端 未结 10 2371
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 22:55

I have a list like this:

List list = new List

How to handle adding new position to this list?

When

10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 23:27

    To piggy-back off Ahmad's use of Extension Methods, you can create your own class where the list is private with a public get method and a public add method.

    public class MyList
    {
        private List PrivateSomeClassList;
        public List SomeClassList
        {
            get
            {
                return PrivateSomeClassList;
            }
        }
    
        public void Add(SomeClass obj)
        {
            // do whatever you want
            PrivateSomeClassList.Add(obj);
        }
    }
    

    However, this class only provides access to List<> methods that you manually expose...hence may not be useful in cases where you need a lot of that functionality.

提交回复
热议问题