Search code examples
c#enumeratorgeneric-collections

Generic Collections C#


I have implemented a generic custom collection class which only takes a Person type object.

While providing support for the Enumerator to iterate through the collection it shows an error

Cannot apply indexing with [] to an expression of type 'CustomCollection.CustomCollection'

Below is the code snippet for the class I created.

public class CustomCollection<T> : ICollection<T> where T : Person
{
    private IList<T> lst = null;

    public CustomCollection()
    {
        lst = new List<T>();
    }

    public void Add(T item)
    {
        this.lst.Add(item);
    }

    public void Clear()
    {
        this.lst.Clear();
    }

    public bool Contains(T item)
    {
        bool result = false;

        foreach (T obj in this.lst)
        {
            if (obj.Id.Equals(item.Id))
            {
                result = true;
                break;
            }
        }

        return result;
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        throw new NotImplementedException();
    }

    public int Count
    {
        get { return this.lst.Count; }
    }

    public bool IsReadOnly
    {
        get { return false; }
    }

    public bool Remove(T item)
    {
        bool result = false;

        foreach (T obj in this.lst)
        {
            if (obj.Id.Equals(item.Id))
            {
                this.lst.Remove(item);
            }
        }

        return result;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return new PersonEnumerator<T>(this);
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

The Enumerator class as below:

public class PersonEnumerator<T> : IEnumerator<T> where T : Person
{
    private CustomCollection<T> collection = null;
    private int index;
    private T current;

    public PersonEnumerator(CustomCollection<T> collection)
    {
        this.collection = collection;
        this.index = -1;
        current = default(T);
    }

    public T Current
    {
        get { return this.current; }
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    object IEnumerator.Current
    {
        get { return this.Current; }
    }

    public bool MoveNext()
    {
        if (++this.index >= collection.Count)
        {
            return false;
        }
        else
        {
            this.current = collection[this.index];
        }

        return true;
    }

    public void Reset()
    {
        this.index = -1;
        current = default(T);
    }
}

Person Class only contains FirstName, LastName & Id as properties in it.


Solution

  • ICollection doesn't implements an indexer. If you want to use indexing, you should implement IList instead.

    You can use Linq with ICollection:

    var result = Items.ElementAt(index);
    

    Or you can add your own indexer:

    public T this[int i]
    {
        return Items[i];
    }