实现IList接口

21

我对泛型不熟悉。我想通过继承 IList<T> 接口来实现自己的集合。

请您提供一个实现了 IList<T> 接口的类的链接,或者提供一个至少实现了 AddRemove 方法的代码吗?

6个回答

36
除了继承自List<T>,您还可以使用外观模式List<T>并向您的外观类添加更多功能。
class MyCollection<T> : IList<T>
{
    private readonly IList<T> _list = new List<T>();

    #region Implementation of IEnumerable

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

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    #endregion

    #region Implementation of ICollection<T>

    public void Add(T item)
    {
        _list.Add(item);
    }

    public void Clear()
    {
        _list.Clear();
    }

    public bool Contains(T item)
    {
        return _list.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }

    public bool Remove(T item)
    {
        return _list.Remove(item);
    }

    public int Count
    {
        get { return _list.Count; }
    }

    public bool IsReadOnly
    {
        get { return _list.IsReadOnly; }
    }

    #endregion

    #region Implementation of IList<T>

    public int IndexOf(T item)
    {
        return _list.IndexOf(item);
    }

    public void Insert(int index, T item)
    {
        _list.Insert(index, item);
    }

    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
    }

    public T this[int index]
    {
        get { return _list[index]; }
        set { _list[index] = value; }
    }

    #endregion

    #region Your Added Stuff

    // Add new features to your collection.

    #endregion
}

但在这种情况下,我无法将我的集合绑定到DataGridView,而是必须在MyCollection中公开_list成员。 - Ashish Ashu
这样做有什么缺点? - Dave Neeley
2
抱歉回复晚了,但是 IEnumerator<T> GetEnumerator 返回 GetEnumerator 的部分让我有些困惑。这看起来像是一个循环引用。为什么不会导致堆栈溢出异常呢? - user153923
@jp2code 这不是一个无限循环。它正在调用 IEnumerator<T> GetEnumerator()。我已经在示例中添加了一条注释来说明这一点。 - Jim
IList<T>也实现了IList吗?我以为会的,但我收到了编译器错误。我猜List<T>可以,但扩展MyCollection使其可以转换为IList意味着还要实现该接口吗? - gdbj
马什威格尔的回答 使用VS自动生成了上述大部分内容。 - ToolmakerSteve

16

除非你有非常充分的理由需要这样做,否则最好从System.Collections.ObjectModel.Collection<T>继承,因为它拥有你所需的一切。

请注意,虽然实现IList<T>的人不需要将this[int](索引器)实现为O(1)(基本上是常数时间访问),但强烈建议你这样做。


15
Visual Studio提供了像IList<>这样的接口的自动完整工作实现
您只需编写类似以下代码即可:
public class MyCollection<T> : IList<T>
{
    // This line is important. Without it the auto implementation creates only
    // methods with "NotImplemented" exceptions
    readonly IList<T> _list = new List<T>();
}

(当行

readonly IList<T> _list = new List<T>(); 

是重要的部分!

enter image description here

然后点击灯泡符号或者将光标放在IList<>上并按下Strg + "." ,会弹出几个可用的实现,例如:

enter image description here


4
正是我所需要的。应该接受这个答案,因为这是正确的做法。 - JasonG

1
你可以查看Mono项目。这里提供了完整的源代码,所以你可以看看一些类是如何实现的。例如,“System.Collections.Generics.List<T>”。

2
http://anonsvn.mono-project.com/viewvc/trunk/mcs/class/corlib/System.Collections.Generic/List.cs?view=markup - Stobor
打开 Mono 项目的主页后,我应该下载什么? - Ashish Ashu
更新了 Mono 类的链接 - https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Collections.Generic/List.cs - David Gardiner

1
在大多数情况下,您可以直接使用 List<T> 或从 List<T> 派生。 如果您从 List<T> 派生,则会自动获得 Add 和 Remove 的实现。

0
继承自List通常是最快的方法,但如果您需要从另一个类(例如ContextBoundObject等)继承,则以后可能会受到限制。实现IList非常快速,正如上面指出的那样,它提供了更多的灵活性。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接