icollection


The icollection interface is inherited by all classes in the System.Collections namespace.  It extends the IEnumerable interface.

The icollection interface has three public properties:

Count – Returns the count of elements in the collection

IsSynchronized – Returns whether the collection is thread safe.

SyncRoot – Returns an object to allow the caller to synchronize access to the collection

It also has a public method:

CopyTo – Copies the items in the collection to an array.

You must also implement the GetEnumerator method because the icollection interface extends the ienumerable interface.

Here is an example of implementing the icollection interface:

public class Car
{       
    public string Model;
    public decimal Price;
    //constructor
    public Car(string strModel, decimal decPrice)
    {
        this.Model = strModel;
        this.Price = decPrice;       
    }
}

public class AutoCollection : ICollection
{
    private Car[] _autos;
    private int _numberOfCars = 0;

    //constructor
    public AutoCollection(Car[] carArray)
    {
        _numberOfCars = carArray.Length;
        //set the length of our private array
        _autos = new Car[_numberOfCars];
       //load the private array
        for (int i = 0; i < carArray.Length; i++)
        {
            _autos[i] = carArray[i];
        }
    }

    //Must implement the Count property to return
    //the number of items in your collection

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

    //Must implement the CopyTo method to allow items to be
    //copied to an array

    public virtual void CopyTo(Array ToArray, int index)
    {
        foreach (object o in this)
        {
            ToArray.SetValue(o, index);
            index++;
        }       
    }

    //Must implement the SyncRoot property to return
    //the current object so that the caller can synchronize
    //access to the collection.

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

    //Must implement the IsSynchronized property. Return true if the
    //collection is thread safe; otherwise, return false.

    public bool IsSynchronized
    {
        get
        {
            return false;
        }
    }

    //Must implement the GetEnumerator method of the IEnumerable interface
    //to return an Enumerator

    IEnumerator IEnumerable.GetEnumerator()
    {
        return new AutosEnum(_autos);
    }

}

public class TestICollectionImp
{
    public void TestICollection()
    {
        //Load a little array to pass to my AutoCollection
        //class constructor

        Car[] carArray = new Car[3]
        {
            new Car("Camry", 28000),
            new Car("Jetta", 24500),
            new Car("Civic", 22000)
        };

        //Initialize the AutoCollection class
        AutoCollection autoColl = new AutoCollection(carArray);

        //Test the Count property
        int testCount = autoColl.Count;

        System.Diagnostics.Debug.WriteLine(testCount);

        //Test the SyncRoot property
        lock (autoColl.SyncRoot)
        {
           // Access the collection.
            System.Diagnostics.Debug.WriteLine("locked");
        }

        //Test the IsSynchronized property
        bool checkSynch = autoColl.IsSynchronized;
        System.Diagnostics.Debug.WriteLine(checkSynch);

       //Test the CopyTo method
        Car[] CarArray2 = new Car[autoColl.Count];
        autoColl.CopyTo(CarArray2, 0);

        foreach (Car car in CarArray2)
        {
            System.Diagnostics.Debug.WriteLine(car.Model);
        }
    }
}

No comments:

Post a Comment