Index based collections are sequenced collections that are accessed via an integer index. The example below shows the design pattern, implementers need only implement the methods that are appropriate to their collection. The List class is the default implementation of this pattern.
class <Item>List : IList, ICollection { // ICollection void ICollection.CopyTo(Array array, int index); //private impl. IEnumerator ICollection.GetEnumerator(); //private implementation int Count{get;}; Object ICollection.SyncRoot {get;}; //private implementation boolean ICollection.IsSynchronized {get;}; //private implementation // IList Object IList.this[int index] {get; set;} //private implementation void IList.Add (Object value); //private implementation void Clear(); int IList.IndexOf (Object value); //private implementation void IList.Insert(int index, Object value); //private implementation void RemoveAt(int index); void Remove(Object value); //private implementation boolean Contains (Object value); //private implementation // <item> Design Pattern boolean Contains (<item> value); public <item> this[int index]; // default index (set,get) //can’t insert greater than length. //ArgumentOutOfRange exception thrown if out of range. //ReadOnly collections only public void CopyTo(<item>[] array, int index); //ReadWrite only public int Add(<item> value); //adds at end //ReadWrite only public void AddRange(<item>[] array);//ReadWrite only public void Insert(int index, <item> value); //insert,ReadWrite only public int IndexOf (<item> value); public <item>[] ToArray (); public void Remove (Object value); //ReadWrite only
Implementing IList is optional but highly recommended. If you are implementing a collection around a value type, you should consider using strongly typed collections for efficiency. For Read only collections, the appropriate indicated methods should throw an InvalidOperationException.