zoukankan      html  css  js  c++  java
  • IEnumerable,ICollection,IList,List 比较

    IEnumerable is the base interface that the following extend or implement. It doesn't allow for direct access and is readonly. So use this only if you intend to iterate over the collection.

    ICollection extends IEnumerable but in addition allows for adding, removing, testing whether an element is present in the collection and getting the total number of elements. It doesn't allow for directly accessing an element by index. That would be an O(n) operation as you need to start iterating over it until you find the corresponding element.

    IList extends ICollection (and thus it inherits all its properties) but in addition allows for directly accessing elements by index. It's an O(1) operation.

    List is just a concrete implementation of the IList interface.

    In your code you should always expose the type that's highest in the object hierarchy that will correspond to the needs of the callers. So for example if the callers are only going to enumerate over the dataset, use IEnumerable. If they need to have direct access to elements by index expose an IList.

    List should only be used internally by your code but usually not present in the signature of the methods you are exposing. This gives you more flexibility as you could easily swap the concrete implementation without breaking the contract.

  • 相关阅读:
    linux内核编译
    字符设备驱动ioctl实现用户层内核层通信
    Linux内核完全剖析基于0.12内核
    KVM分析报告
    kvm的vmcall
    kvm源代码分析
    KVM基本概念
    linux系统调用
    UML的9种图例解析(转)
    SurfaceView的基本使用(转)
  • 原文地址:https://www.cnblogs.com/wesson2019-blog/p/13568617.html
Copyright © 2011-2022 走看看