ICollection<T> 的简单现有实现
Simple existing implementation of ICollection<T>
.NET Framework 中是否有 ICollection<T>
的简单实现? IE。一个集合 class 能够添加和删除项目,但没有索引。 Collection<T>
绝对不适合,因为它也实现了 IList
,因此可以通过索引访问元素。
将 Collection<T>
或 List<T>
公开为 ICollection<T>
在我的情况下也不起作用,因为我需要从中继承我自己的 class,并且 class 继承自实现 IList<T>
的任何其他 class 也将具有索引。
我知道自己实现一个没什么大不了的,只是觉得应该已经存在了,搜索了一下也没有找到类似的。
Hashset<T>
如果您希望它是无序的且具有唯一值,应该可以使用。
如评论中所述,ICollection<T>
是一个更简单的集合,它是无序的,并且允许重复条目。 MSDN
ICollection<string> unordered = new Collection<string>();
最后,我不知道 "simple" 具有 .Add() / .Remove() 功能但不公开索引的集合的本机 .NET 实现。所以,要回答你的问题,看起来你必须自己推出你的利基功能。
只是给出一个快速的区别:
排序列表
是通过获取键而不是索引来使用的最佳方法,它基于二进制搜索。在内部,它使用两个列表:IList 和 IList。它不使用字典。因此它没有任何哈希值。
SortedDictionary 与 SortedList 相同。但是,不同之处在于内部发展。 Sorted Dictionary 使用的是 B 树。因此修改速度很快,查找与排序列表相同。
HashSet和List的区别在于HashSet保证了唯一性。这意味着如果您尝试两次添加该值,它将忽略该值而不会给出任何错误或重复相同的值。
因此,如果您不想使用基于索引,那么您可以使用继承自 ICollection 的 SortedList,然后使用 IEnumerable...
否则 HashSet 是唯一性很重要的最佳选择。
这是在 System.Collections
命名空间中实现 ICollection<T>
的 类 列表:
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>
作为要包装的集合类型:
class UniqueCollection<T> : SimpleCollection<T>
{
public UniqueCollection() : base(new HashSet<T>()) {}
}
.NET Framework 中是否有 ICollection<T>
的简单实现? IE。一个集合 class 能够添加和删除项目,但没有索引。 Collection<T>
绝对不适合,因为它也实现了 IList
,因此可以通过索引访问元素。
将 Collection<T>
或 List<T>
公开为 ICollection<T>
在我的情况下也不起作用,因为我需要从中继承我自己的 class,并且 class 继承自实现 IList<T>
的任何其他 class 也将具有索引。
我知道自己实现一个没什么大不了的,只是觉得应该已经存在了,搜索了一下也没有找到类似的。
Hashset<T>
如果您希望它是无序的且具有唯一值,应该可以使用。
如评论中所述,ICollection<T>
是一个更简单的集合,它是无序的,并且允许重复条目。 MSDN
ICollection<string> unordered = new Collection<string>();
最后,我不知道 "simple" 具有 .Add() / .Remove() 功能但不公开索引的集合的本机 .NET 实现。所以,要回答你的问题,看起来你必须自己推出你的利基功能。
只是给出一个快速的区别:
排序列表
是通过获取键而不是索引来使用的最佳方法,它基于二进制搜索。在内部,它使用两个列表:IList 和 IList。它不使用字典。因此它没有任何哈希值。
SortedDictionary 与 SortedList 相同。但是,不同之处在于内部发展。 Sorted Dictionary 使用的是 B 树。因此修改速度很快,查找与排序列表相同。
HashSet和List的区别在于HashSet保证了唯一性。这意味着如果您尝试两次添加该值,它将忽略该值而不会给出任何错误或重复相同的值。
因此,如果您不想使用基于索引,那么您可以使用继承自 ICollection 的 SortedList,然后使用 IEnumerable...
否则 HashSet 是唯一性很重要的最佳选择。
这是在 System.Collections
命名空间中实现 ICollection<T>
的 类 列表:
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>
作为要包装的集合类型:
class UniqueCollection<T> : SimpleCollection<T>
{
public UniqueCollection() : base(new HashSet<T>()) {}
}