Implementing IList interface

后端 未结 6 1192
温柔的废话
温柔的废话 2020-12-09 11:29

I am new to generics. I want to implement my own collection by deriving it from IList interface.

Can you please provide me some link to a class

6条回答
  •  渐次进展
    2020-12-09 11:47

    In addition to deriving from List, you can facade List and add more features to your facade class.

    class MyCollection : IList
    {
        private readonly IList _list = new List();
    
        #region Implementation of IEnumerable
    
        public IEnumerator GetEnumerator()
        {
            return _list.GetEnumerator();
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    
        #endregion
    
        #region Implementation of ICollection
    
        public void Add(T item)
        {
            _list.Add(item);
        }
    
        public void Clear()
        {
            _list.Clear();
        }
    
        public bool Contains(T item)
        {
            return _list.Contains(item);
        }
    
        public void CopyTo(T[] array, int arrayIndex)
        {
            _list.CopyTo(array, arrayIndex);
        }
    
        public bool Remove(T item)
        {
            return _list.Remove(item);
        }
    
        public int Count
        {
            get { return _list.Count; }
        }
    
        public bool IsReadOnly
        {
            get { return _list.IsReadOnly; }
        }
    
        #endregion
    
        #region Implementation of IList
    
        public int IndexOf(T item)
        {
            return _list.IndexOf(item);
        }
    
        public void Insert(int index, T item)
        {
            _list.Insert(index, item);
        }
    
        public void RemoveAt(int index)
        {
            _list.RemoveAt(index);
        }
    
        public T this[int index]
        {
            get { return _list[index]; }
            set { _list[index] = value; }
        }
    
        #endregion
    
        #region Your Added Stuff
    
        // Add new features to your collection.
    
        #endregion
    }
    

提交回复
热议问题