C# ICollection的简单现有实现<;T>;

C# ICollection的简单现有实现<;T>;,c#,.net,generics,C#,.net,Generics,在.NET framework中是否有ICollection的简单实现?即,一个集合类,能够添加和删除项,但不需要索引Collection显然不适合,因为它还实现了IList,因此可以通过索引访问元素 在我的情况下,将集合或列表显示为ICollection也不起作用,因为我需要从中继承我自己的类,并且从实现IList的任何其他类继承的类也将具有索引 我知道自己实现一个并没有什么大不了的,但我只是认为它应该已经存在,搜索了但没有找到任何类似的东西。Hashset Hashset<T>

在.NET framework中是否有
ICollection
的简单实现?即,一个集合类,能够添加和删除项,但不需要索引
Collection
显然不适合,因为它还实现了
IList
,因此可以通过索引访问元素

在我的情况下,将
集合
列表
显示为
ICollection
也不起作用,因为我需要从中继承我自己的类,并且从实现
IList
的任何其他类继承的类也将具有索引

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

Hashset
Hashset<T> 
如果您希望它以唯一值无序排列,则应该可以使用

如注释中所述,
ICollection
是一个更简单的集合,它是无序的,允许重复条目

ICollection unordered=new Collection();

最后,我不知道有一个集合的“简单”本机.NET实现具有.Add()/.Remove()功能而不公开索引。因此,要回答您的问题,您的利基功能似乎需要自己推出。

只需简单介绍一下差异:

分类表

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

SortedDictionary与SortedList相同。然而,区别在于内部发展。排序字典使用B树。因此修改速度很快,查找和排序列表相同

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

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


否则,哈希集是唯一性非常重要的最佳选项。

以下是在
系统中实现
ICollection
的类列表。集合
命名空间:

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>
System.Collections.Concurrent.ConcurrentDictionary
System.Collections.Generic.Dictionary
System.Collections.Generic.HashSet
System.Collections.Generic.LinkedList
System.Collections.Generic.List
System.Collections.Generic.SortedDictionary
System.Collections.Generic.SortedList
System.Collections.Generic.SortedSet
System.Collections.ObjectModel.Collection
System.Collections.ObjectModel.ReadOnlyCollection
System.Collections.ObjectModel.ReadOnlyDictionary
System.Collections.ObjectModel.WeakradonlyCollection
但所有这些实现都添加了额外的功能,而且由于您希望从实现继承,而只公开
ICollection
方法,因此使用其中任何一种方法都不是一个真正的选项

你唯一的选择就是实现你自己的。这很容易做到。您只需要包装一个合适的
ICollection
实现。这里有一个默认情况下使用
列表
,但也允许派生类使用特定类型的
ICollection

类SimpleCollection:ICollection
{
i收集项目;
公共SimpleCollection(){
//默认使用列表。
_项目=新列表();
}
受保护的SimpleCollection(ICollection集合){
//让派生类指定要包装的ICollection的确切类型。
_项目=收集;
}
公共无效添加(T项){
_项目。添加(项目);
}
公共无效清除(){
_items.Clear();
}
公共bool包含(T项){
返回项目。包含(项目);
}
public void CopyTo(T[]数组,int arrayIndex){
_items.CopyTo(数组、数组索引);
}
公共整数计数
{
获取{return\u items.Count;}
}
公共图书馆是只读的
{
获取{return false;}
}
公共布尔删除(T项)
{
返回项目。删除(项目);
}
公共IEnumerator GetEnumerator()
{
返回_items.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回_items.GetEnumerator();
}
}
这超出了您所追求的范围,但是,例如,如果您希望存储唯一的项,您可以从中派生并提供一个
HashSet
作为要包装的集合类型:

class UniqueCollection<T> : SimpleCollection<T>
{
    public UniqueCollection() : base(new HashSet<T>()) {}
}
类唯一集合:SimpleCollection
{
public UniqueCollection():base(新HashSet()){}
}

HashSet
是非常特定的集合,其添加可能不需要添加项。。。我不会将其用作“简单实现”示例…
HashSet
要求每个项目都是唯一的,而
ICollection
可能包含重复的项目。
IList
未排序。同意Cory<代码>IList只是一个接口。订购需要实施;
IList
一点也不表明它是有序的。添加到
集合
中的项很可能以添加它们时的相同顺序存储。在.Net framework中没有提供项的真正随机排序的集合。。。当你说像once这样的列表不起作用,因为它们是“有序的”时,请澄清你到底在寻找什么。我的错误是,
IList
确实没有有序。但它允许通过索引访问项目,这在我的例子中是不需要的。我不需要随机顺序,只是不允许通过索引访问它。将编辑问题以澄清。然后用您自己的类重写
集合
,并短路您不想公开的任何方法。或者,正如David L.在下面指出的,创建一个
ICollection
usi的实例
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();
    }
}
class UniqueCollection<T> : SimpleCollection<T>
{
    public UniqueCollection() : base(new HashSet<T>()) {}
}