Home

Collection Classes:
The ICollection Interface

 

Introduction

One of the primary pieces of information you should provide about a the values in a database is the number of values that a list is (currently) holding. When creating a collection class, to prepare it to provide this valuable information, you can (should) implement an interface named ICollection. The ICollection interface is defined in the System.Collections namespace while its equivalent of the same name is defined in the System.Collections.Generic namespace. This means that, if you are creating a class that implements it, you should include this namespace in the file. Here is an example for the System.Collections.ICollection interface:

 
using System;
using System.Collections;

namespace BookCollection
{
    public class Collection : ICollection
    {
    }
}

Implementing ICollection

To assist you with keeping track of the number of items in a collection, the ICollection interface is equipped with a property named Count, which you must implement. To do this, you can create a private member variable that will actually keep a count of the number of items. The Count property can then be used to communicate this information to the clients of the class. Here is an example:

using System;
using System.Collections;

namespace BookCollection
{
    public class Collection : ICollection
    {
        private int NumberOfBooks;

        public Collection()
        {
            NumberOfBooks = 0;
        }

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

The ICollection interface also allows its implementer to copy some of its items to an array. To provide this functionality, the interface is equipped with a method named CopyTo, which you must implement. The syntax of this method is:

void CopyTo(Array array, int index);

This method takes two arguments. The first argument is the array that will receive the items. The second argument is the index of the item from where the copying operation will begin. Here is an example:

using System;
using System.Collections;

namespace BookCollection
{
    public class Collection : ICollection
    {
        . . . No Change

        public virtual void CopyTo(Array items, int index)
        {
            string[] bks = new string[Count];

            for (int i = 0; i < Count; i++)
                bks[i] = books[i];
            items = bks;
        }
    }
}

If you create a collection class, you can provide the ability to enumerate its items. When this is done, some time to time, you will want to identify or to know what item is currently being accessed. In case other collection classes are using the same function at the time you are accessing this information, you should have an object that is responsible for synchronizing the collection. To do this in your ICollection-based class, you must implement a property named SyncRoot. This property must return an Object object. Here is an example:

using System;
using System.Collections;

namespace BookCollection
{
    public class Collection : ICollection
    {
        . . . No Change

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

Besides the ability to specify the number of items in a collection, a class that implements the ICollection interface must retrieve a value that indicates whether its item is synchronized. To give this information, you must implement a Boolean property named IsSynchronized. Here is an example:

using System;
using System.Collections;

namespace BookCollection
{
    public class Collection : ICollection
    {
        . . . No Change

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

System.Collections.ICollection (and System.Collections.Generic.ICollection) extends the IEnumerable interface. This means that you should be able to use foreach in your ICollection-based class but you must create the functionality yourself, which is done by implementing the GetEnumerator() method. Even if you don't want to support this feature, you still must provide at least a skeleton for this method. Here is an example:

using System;
using System.Collections;

namespace BookCollection
{
    public class Collection : ICollection
    {
        private int NumberOfBooks;
        private string[] books;

        public Collection()
        {
            NumberOfBooks = 0;
            books = new string[5];
        }

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

        public virtual void CopyTo(Array items, int index)
        {
            string[] bks = new string[Count];

            for (int i = 0; i < Count; i++)
                bks[i] = books[i];
            items = bks;
        }

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

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

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

Previous Copyright © 2007 FunctionX, Inc. Next