简体   繁体   English

使用IEnumerable和ICollection与IList的自定义集合

[英]Custom Collection using IEnumerable vs ICollection vs IList

I need to design my own custom GenericCollection class. 我需要设计自己的自定义GenericCollection类。 Now i have plenty of options to derive it using IEnumerable , ICollection , and IList , where later offers some added functionalities. 现在我有很多选项可以使用IEnumerableICollectionIList派生它,后来提供了一些附加功能。

I am little confused that if i go with IEnumerable<T> i might require declaring the object to actually hold the collection like in this case _list . 我很困惑,如果我使用IEnumerable<T>我可能需要声明对象实际上保存集合,就像在这种情况下_list

public class GenericCollection<T> : IEnumerable<T>
{
    private List<T> _list;
    //...
}

But if i go with ICollection<T> or IList<T> , i do not require to declare the List object as it is implicitly available. 但是如果我使用ICollection<T>IList<T> ,我不需要声明List对象,因为它是隐式可用的。

public class GenericCollection<T> : IList<T>
{
    // no need for List object
    //private List<T> _list; 
    //...
}

What is the difference between these two approaches with respect to performance ? 这两种方法在性能方面有何不同?

In which scenario each one is preferred especially when it comes to designing your own collection. 在哪种情况下,每个人都是首选,特别是在设计自己的集合时。 I am interested in the light weight collection with good performance. 我对轻量级收藏感兴趣,性能良好。 I think this can be achieved using IEnumerable<T> but how exactly along with some strong reasons to go with it? 我认为这可以使用IEnumerable<T>来实现,但是如何与一些强有力的理由一起使用呢?

I have reviewed some existing posts but none is giving required information. 我已经审查了一些现有的帖子,但没有提供所需的信息。

Returning 'IList' vs 'ICollection' vs 'Collection' 返回'IList'与'ICollection'对比'收集'

IEnumerable , ICollection , and IList (generally, any type with an I prefix) are just interfaces . IEnumerableICollectionIList (通常,任何带有I前缀的类型)都只是接口 They let you expose what your class will do, but unlike if you inherit a class, interfaces do not provide you a default implementation of any of the things they say you must do. 它们允许您公开您的类将要执行的操作,但与继承类不同,接口不会为您提供他们必须执行的任何操作的默认实现。

As far as choosing which interface, here's a quick guide: 至于选择哪个界面,这里有一个快速指南:

  • An IList is an ICollection that can be accessed by index. IList是可以通过索引访问的ICollection
  • An ICollection is an IEnumerable with easy access to things like Add , Remove , and Count . ICollection是一个IEnumerable ,可以轻松访问AddRemoveCount
  • An IEnumerable is anything that can be enumerated, even if the list of those things doesn't exist until you enumerate it. IEnumerable是可以枚举的任何内容,即使这些内容的列表在您枚举之前不存在也是如此。

Some classes that you might want to extend (or keep as a private field that runs most of the logic) for your collection are List<T> , Collection<T> , (which implements IList<T> , but with easier access to overriding implementation, see Collection<T> versus List<T> what should you use on your interfaces? for the big differences between these two) ObservableCollection<T> , or collections that are not lists, like Dictionary<T, U> and HashSet<T> . 您可能希望扩展(或保留为运行大多数逻辑的私有字段)的一些类是List<T>Collection<T> ,(它实现IList<T> ,但更容易访问覆盖实现,请参阅Collection <T>与List <T>您应该在接口上使用什么?这两者之间的巨大差异) ObservableCollection<T> ,或非List的集合,如Dictionary<T, U>HashSet<T> For more info on any of these, look up the MSDN documentation on the class. 有关其中任何一项的更多信息,请查阅该课程上的MSDN文档。

First off, you don't have to actually choose betwen these interfaces, if it's necessary you can implement all three. 首先,您不必实际选择这些接口,如果有必要,您可以实现这三个接口。 Second, implementing IEnumerable does not require you to make the underlying list public. 其次,实现IEnumerable并不要求您公开底层列表。 You can implement just the methods to use the Enumerator of the underlying list. 您可以只实现方法来使用基础列表的枚举器。

Performancewise, I doubt there'll be much of an impact, focus on what you need functionally. 从表面上看,我怀疑会产生很大的影响,专注于你在功能上需要什么。 The only way to know for sure is to measure. 确切知道的唯一方法是衡量。

The performance is unlikely to be dependent on which interfaces are implemented. 性能不太可能取决于实现哪些接口。 It rather depends on how many instructions the processor has to run to achieve a certain goal. 它取决于处理器为实现某个目标而必须运行多少指令。 If you implement IEnumerable and wrap over the List, you are likely to end up writing Add/Remove/this[] methods that are just propagating the calls to the List, which would add a performance overhead. 如果您实现IEnumerable并包裹List,您可能最终会编写Add / Remove / this []方法,这些方法只是将调用传播到List,这会增加性能开销。 Hence, although I didn't take any measurements, the inheritance approach would likely be a very little bit faster. 因此,尽管我没有进行任何测量,但继承方法可能会更快一些。

However, such details usually matter only for real-time applications with an extreme need to save every possible CPU cycle. 但是,这些细节通常仅适用于需要节省每个可能的CPU周期的实时应用程序。 Eric Lippert has a great article about paying attention to such details: http://blogs.msdn.com/b/ericlippert/archive/2003/10/17/53237.aspx . Eric Lippert有一篇关于关注这些细节的文章很棒: http//blogs.msdn.com/b/ericlippert/archive/2003/10/17/53237.aspx Generally, you are likely to be better off using the approach that better fits business logic and architecture of your application, rather than performance details. 通常,您可能最好使用更适合应用程序的业务逻辑和体系结构的方法,而不是性能详细信息。

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

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