【问题标题】:Generic Collections C#泛型集合 C#
【发布时间】:2015-05-05 02:23:50
【问题描述】:

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

在为 Enumerator 迭代集合提供支持时,它会显示错误

无法将带有 [] 的索引应用于“CustomCollection.CustomCollection”类型的表达式

下面是我创建的类的代码 sn-p。

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

Enumerator 类如下:

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

【问题讨论】:

标签: c# enumerator generic-collections


【解决方案1】:

ICollection 没有实现索引器。如果你想使用索引,你应该实现IList。

您可以将Linq 与ICollection 一起使用:

var result = Items.ElementAt(index);

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

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

【讨论】:

    【解决方案2】:

    要构建您的代码,您需要将indexer 添加到您的CustomCollection&lt;T&gt; 类中,例如

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

    那么你可以说使用collection[n] 来获取MoveNext() 方法中的第nth 元素。


    或者

    您可以只公开lst.GetEnumerator(),而不是让您的PersonEnumerator&lt;T&gt; 类返回IEnumerator&lt;T&gt;,即

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

    除非你打算用你的枚举器做任何花哨的事情,否则你最好使用lst。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      相关资源
      最近更新 更多