泛型集合 C#

Generic Collections C#

我已经实现了一个通用的自定义集合 class,它只接受一个 Person 类型的对象。

在为枚举器循环访问集合提供支持时显示错误

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

下面是我创建的 class 的代码片段。

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();
    }

枚举器class如下:

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 仅包含 FirstName、LastName 和 Id 作为其中的属性。

ICollection doesn't implements an indexer. If you want to use indexing, you should implement IList 代替。

您可以将 LinqICollection 一起使用:

var result = Items.ElementAt(index);

或者您可以添加自己的索引器:

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

要构建代码,您需要将 indexer 添加到 CustomCollection<T> class,例如

public T this[int index]
{
    return lst [index];
}

然后你可以说使用collection[n]MoveNext()方法中得到第nth个元素。


而不是让你的 PersonEnumerator<T> class 你可以公开 lst.GetEnumerator() 其中 returns 和 IEnumerator<T>,即

public IEnumerator<T> GetEnumerator()
{
    return lst.GetEnumerator();
}

除非您打算对您的枚举器做任何花哨的事情,否则您不妨将其用于 lst