简体   繁体   English

什么是继承ICollection的最基本的类<T>

[英]What is the most basic class that inherits ICollection<T>

I need a generic collection class which I can add to, and enumerate over. 我需要一个通用的集合类,我可以添加,并枚举。 Since ICollection<T> inherits from IEnumerable<T> , the class really just needs to inherit from ICollection<T> . 由于ICollection<T>继承自IEnumerable<T> ,因此该类实际上只需从ICollection<T>继承。 Is there a simple generic class in the BCL that just inherits ICollection<T> like a generic version of CollectionBase? BCL中是否有一个简单的泛型类只是像CollectionBase的泛型版一样继承ICollection<T> If not, then what class comes closest? 如果没有,那么哪个班级最接近?

I would guess List<T> which is what I've been using but i don't need to sequential aspect. 我猜List<T>这是我一直在使用但我不需要顺序方面。 Is there anything better (by which I mean [smaller memory footprint/faster access/simpler])? 有什么更好的(我的意思是[更小的内存占用/更快的访问/更简单])? Bag would be perfect if it existed. 如果它存在,袋子将是完美的。

EDIT 1: In my particular instance, I'm .concat ing to another IEnumerable, querying it, and then displaying the results (in no particular order). 编辑1:在我的特定实例中,我是.concat到另一个IEnumerable,查询它,然后显示结果(没有特定的顺序)。 I'm not attempting to make my own class. 我不是想自己上课。 I've just needed to make a throwaway collection so many times, that I thought it would be useful to find the best throwaway to use. 我只需要多次制作一次性收藏品,我认为找到最好的一次性使用会很有用。 Because I feel I've done something similar so many times, I felt I should keep this question as generic as possible (no pun intended), I know better now. 因为我觉得我已经做了很多次类似的事情,我觉得我应该尽可能保持这个问题的通用性(没有双关语意),我现在知道的更好。

EDIT 2: Thanks for everybody's answers, As @BlueRaja pointed out, any simple class is going to have about the same overhead, and thus I think I will be sticking with my original ways of using List<T> . 编辑2:感谢大家的回答,正如@BlueRaja指出的那样,任何简单的类都会有相同的开销,因此我认为我会坚持使用List<T>原始方法。 Since they are all about the same, my silly reasons of "It's easier to type", and "I don't have to bring in yet another using" aren't such bad reasons. 由于它们大致相同,我的“更容易打字”的愚蠢理由,以及“我不必带来另一种使用”并不是很糟糕的原因。

[smaller memory footprint/faster access/simpler] [更小的内存占用/更快的访问/更简单]

They are all going to have pretty much the same memory footprint, and if you use ICollection the interface will not change. 它们将具有几乎相同的内存占用,如果使用ICollection,接口将不会更改。

What really matters is which will scale best for the operations you need: Linked-list does better appending/removal (of head/tail elements), while an array-based list has random-access. 真正重要的是哪种方法最适合您所需的操作:Linked-list可以更好地追加/删除(头/尾元素),而基于数组的列表具有随机访问权限。 There are other structures too - which you should use depends on your application. 还有其他结构 - 您应该使用它取决于您的应用程序。

You'll probably want to look into Collection<T> . 您可能想要查看Collection<T> It was designed for the express purpose of subclassing, as the documentation indicates: 它是为子类化的明确目的而设计的,正如文档所示:

Provides the base class for a generic collection. 提供泛型集合的基类。

Having said that, any of the collections are fine; 话虽如此,任何收藏都很好; I've inherited from List<T> , Stack<T> and so on; 我继承自List<T>Stack<T>等等; pick whichever one is closest to the functionality you actually need. 选择最接近您实际需要的功能的那个。

Smaller and faster all depends on what exactly you're doing and what your needs are. 更小更快取决于您正在做什么以及您的需求是什么。 The only other class I might recommend is LinkedList<> which implements ICollection<> . 我可能推荐的唯一其他类是LinkedList<> ,它实现了ICollection<>

You could use Reflector to check the .NET FCL and see what classes use that collection. 您可以使用Reflector检查.NET FCL并查看哪些类使用该集合。 (There is a search feature that can be started by F3.) (有一个搜索功能可以由F3启动。)

You can also take a look at the C5 Library to see if a collection has already been implemented that meets your needs. 您还可以查看C5库 ,了解是否已经实现了满足您需求的集合。 Check out page 13 of the C5 Manual for the collection interface hierarchy. 有关集合接口层次结构,请参阅C5手册的第13页。

CollectionBase existed primarily to provide a simple mechanism to create typed collections. CollectionBase主要用于提供创建类型集合的简单机制。 With Generics, all collections are now typed. 使用Generics,现在可以输入所有集合。 The vast majority of cases where extensions of CollectionBase used to be used should now be using any of the built-in collections such as List<> or LinkedList<> . 过去使用CollectionBase扩展的绝大多数情况现在应该使用任何内置集合,例如List<>LinkedList<>

Collection<> still exists for those that need to provide a custom collection for reasons other than type (ie, extra validation on add, or some non-standard logic). 对于那些需要提供自定义集合而不是类型的原因(即添加时的额外验证或某些非标准逻辑),仍然存在Collection<> Collection<> is not nearly as commonly used as CollectionBase was and serves a much smaller need. Collection<>并不像CollectionBase那样常用,并且需要的范围要小得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM