ICollection <t> </t>的简单现有实现

时间:2015-01-31 03:33:54

标签: c# .net generics

.NET框架中是否有ICollection<T>的简单实现?即一个集合类,可以添加和删除项目,但不需要编制索引。 Collection<T>肯定不适合,因为它也实现IList,因此可以通过索引访问元素。

Collection<T>List<T>公开为ICollection<T>在我的情况下也不起作用,因为我需要从中继承我自己的类,以及从实现的任何其他类继承的类IList<T>也会有索引。

我知道自己实施一个并不是什么大事,但只是认为它应该已经存在,搜索但没有找到类似的东西。

3 个答案:

答案 0 :(得分:14)

这是在ICollection<T>命名空间中实现System.Collections的类列表:

System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>
System.Collections.Generic.Dictionary<TKey, TValue>
System.Collections.Generic.HashSet<T>
System.Collections.Generic.LinkedList<T>
System.Collections.Generic.List<T>
System.Collections.Generic.SortedDictionary<TKey, TValue>
System.Collections.Generic.SortedList<TKey, TValue>
System.Collections.Generic.SortedSet<T>
System.Collections.ObjectModel.Collection<T>
System.Collections.ObjectModel.ReadOnlyCollection<T>
System.Collections.ObjectModel.ReadOnlyDictionary<TKey, TValue>
System.Collections.ObjectModel.WeakReadOnlyCollection<T>

但是所有这些实现都添加了额外的功能,并且由于您希望从实现继承,但只暴露ICollection<T>方法,因此使用它们中的任何一个都不是真正的选择。

您唯一的选择就是实施自己的选择。这很容易做到。您只需要包含ICollection<T>的合适实现。这是默认情况下使用List<T>的一个,但也允许派生类使用特定类型的ICollection<T>

class SimpleCollection<T> : ICollection<T>
{

    ICollection<T> _items;


    public SimpleCollection() {
        // Default to using a List<T>.
        _items = new List<T>();
    }

    protected SimpleCollection(ICollection<T> collection) {
        // Let derived classes specify the exact type of ICollection<T> to wrap.
        _items = collection;
    }

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

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

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

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

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

    public bool IsReadOnly
    {
        get { return false; }
    }

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

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

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

这超出了你之后的范围,但是,例如,如果你想要存储唯一的项目,你可以从中派生并提供HashSet<T>作为要包装的集合类型:< / p>

class UniqueCollection<T> : SimpleCollection<T>
{
    public UniqueCollection() : base(new HashSet<T>()) {}
}

答案 1 :(得分:3)

Hashset<T> 

如果您希望它与唯一值无序,则应该有效。

See MSDN

正如评论中所提到的,ICollection<T>是一个更简单的集合,它是无序的,并允许重复的条目。 MSDN

ICollection<string> unordered = new Collection<string>();

最后,我不知道&#34;简单&#34;具有.Add()/ .Remove()功能的集合的本机.NET实现,而不暴露索引。所以,为了回答你的问题,看起来你的利基功能你必须自己动手。

答案 2 :(得分:0)

只是为了给出一个快速的差异:

排序列表

通过获取密钥而不是索引来使用的最佳方式是基于二进制搜索。在内部,它使用两个列表:IList和IList。它不使用Dictionary。因此它没有任何哈希值。

SortedDictionary与SortedList相同。但是,区别在于内部发展。 Sorted Dictionary正在使用B Tree。因此修改很快,查找与排序列表相同。

HashSet和List之间的区别是HashSet确保唯一性。这意味着如果您尝试将值添加两次,它将忽略该值而不会给出任何错误或重复该错误。

因此,如果您不想使用基于索引的,那么您可以使用从ICollection继承的SortedList,然后使用IEnumerable ......

否则HashSet是唯一性很重要的最佳选择。