实施IList时不可用的成员

时间:2013-05-25 13:47:31

标签: c#

我试图创建一个实现IList的简单类。但是,除非我首先将DiskBackedCollection转换为IList,否则这些成员不可用。如何在不进行投射的情况下使其可用?

public partial class DiskBackedCollection<T> : IList<T>
{
    private List<T> _underlyingList = new List<T>();

    int IList<T>.IndexOf(T item)
    {
        return _underlyingList.IndexOf(item);
    }

    T IList<T>.this[int index]
    {
        get
        {
            return _underlyingList[index];
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    int ICollection<T>.Count
    {
        get
        {
            return _underlyingList.Count;
        }
    }

    IEnumerator<T> IEnumerable<T>.GetEnumerator()
    {
        return new DiskBackedCollectionEnumerator(this);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return new DiskBackedCollectionEnumerator(this);
    }
}

2 个答案:

答案 0 :(得分:2)

这是因为每个成员都面前有IList<T>.。删除它,它们应该出现。

在前面使用IList<T>.实现接口成员称为explicit implementation

答案 1 :(得分:0)

实现接口要求的方法必须是公开的。你的不是。

此外,您需要删除显式实现:

public int Count
{
  get
  {
    return _underlyingList.Count;
  }
}
相关问题