Implement System.Collections.IList Interface using C#

Today we will look into the interface System.Collections.IList and some of the its methods. A very good example is for IList implementation in use any class Inheriting from IList or ArraList class itself. Following is ArrayList Signature as on MSDN.

public class ArrayList : IList, ICollection, IEnumerable, ICloneable

To start with, let’s derive a simple class from IList

public class ListImpl : IList
{ 
 private object[] _contents = new object[8];
}

We need to implement all members defined in the IList interface and and the interfaces: ICollection and IEnumerable as IList derives from these interfaces. Following is CopyTo Method from ICollection interface.

        public void CopyTo(Array array, int index)
        {
            int j = index;
            for (int i = 0; i < Count; i++)
            {
                array.SetValue(_contents[i], j);
                j++;
            }
        }

Following is GetEnumerator method from IEnumerable interface.

public IEnumerator GetEnumerator()
{
  // Refer to the IEnumerator documentation another sample
  return (_contents as IEnumerable).GetEnumerator();
}

Here are some other methods and properties

      private int _count;
      public int Count
      {
            get
            {
                return _count;
            }
       }

       // Add an object to the List
       public int Add(object value)
       {
           if (_count < _contents.Length)
           {
               _contents[_count] = value;
               _count++;

               return (_count - 1);
           }
           else
           {
                return -1;
           }
       }

       // Clear the List
       public void Clear()
        {
            _count = 0;
        }

       // Check if the List contains some value
       public bool Contains(object value)
        {
            bool inList = false;
            for (int i = 0; i < Count; i++)
            {
                if (_contents[i] == value)
                {
                    inList = true;
                    break;
                }
            }
            return inList;
        }

       // Check if List is fixed size
       public bool IsFixedSize
        {
            get
            {
                return true;
            }
        }

        // Overriding ToString() Method
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (object o in _contents)
            {
                sb.Append(o.ToString());
            }
            return sb.ToString();
        }

We need to provide implementation for following methods as per requirements.


        public int IndexOf(object value)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, object value)
        {
            throw new NotImplementedException();
        }

        public bool IsReadOnly
        {
            get { throw new NotImplementedException(); }
        }

        public void Remove(object value)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        public object this[int index]
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool IsSynchronized
        {
            get { throw new NotImplementedException(); }
        }

        public object SyncRoot
        {
            get { throw new NotImplementedException(); }
        }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s