Home

Collection Classes:
The IList Interface

 

Introduction

While it provides the minimum functionality of a collection, the System.Collections.ICollection (and the System.Collections.Generic.ICollection) interface is not equipped to perform the regular operations of a collection class, such as adding, retrieving, or deleting items from a set.

To assist you with creating a collection class as complete as possible, the .NET Framework provides an interface named IList. The IList interface is defined in the System.Collections namespace and its equivalent of the same name is defined in the System.Collections.Generic namespace. The interface is equipped with the methods necessary to add, insert, delete, or retrieve items from a collection. Because the functionalities of these methods may not suit you, to use these features, you must create a class that implements them.

Implementing IList

As mentioned above, to create a collection, you can derive it from the IList interface. Here is an example:

using System;
using System.Collections;

namespace BookCollection
{
    public class BookList : IList
    {
    }
}

This System.Collections.IList interface is declared as follows:

public interface IList : ICollection, IEnumerable

This System.Collections.Generic.IList interface is declared as follows:

public interface IList<T> : ICollection<T>,
			    IEnumerable<T>, 
			    IEnumerable

This means that the IList interface extends both the ICollection and the IEnumerable interfaces. This also implies that you must implement the members of these parent interfaces. In other words, you must implement the Count property, the SyncRoot property, the IsSynchronized property, and the CopyTo() method of the ICollection interface. From what we learned with ICollection, here are examples of implementing these members for the System.Collections.IList interface:

using System;
using System.Collections;

namespace BookCollection
{
    public class BookList : IList
    {
        private int counter;
        private object[] objs;

        public BookList()
        {
            counter = 0;
            objs = new array[5];
        }

        public virtual int Count
        {
            get { return items; }
        }

        public virtual bool IsSynchronized
        {
            get { return false; }
        }

        public virtual object SyncRoot
        {
            get { return this; }
        }

        public virtual void CopyTo(Array ary, int index)
        {
        }
    }
}

You must also implement the System.Collections.GetEnumerator() (or the System.Collections.Generic.GetEnumerator()) method of the System.Collections.IEnumerable  (or of the System.Collections.Generic.IEnumerable) interface. If you don't have time to completely implement it, you can simply return null. Here is an example for the System.Collections.IList interface:

using System;
using System.Collections;

namespace BookCollection
{
    public class BookList : IList
    {
        . . . No Change

        public IEnumerator GetEnumerator()
        {
            return null;
        }
    }
}
 

Previous Copyright © 2007 FunctionX, Inc. Next