zoukankan      html  css  js  c++  java
  • Asp.Net IEnumerable,ICollection,IList,List区别

    做Asp.Net的同学们,都知道,一类只能有一个继承类,但可以实现多个接口。这句话就告诉我们:IEnumerable,ICollection,IList,List区别了

    首先我看看 IEnumerable:

        // 摘要:
        // 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。
        //
        // 类型参数:
        //   T:
        //   要枚举的对象的类型。
        [TypeDependency("System.SZArrayHelper")]
        public interface IEnumerable<out T> : IEnumerable
        {
            // 摘要:
            //     返回一个循环访问集合的枚举器。
            //
            // 返回结果:
            //     可用于循环访问集合的 System.Collections.Generic.IEnumerator<T>。
            IEnumerator<T> GetEnumerator();
        }

    IEnumerable<T> 实现IEnumerable接口方法,那IEnumberable做什么的,其实就提高可以循环访问的集合。说白了就是一个迭代。

    再来看看ICollection:

        // 摘要:
        //     定义操作泛型集合的方法。
        //
        // 类型参数:
        //   T:
        //     集合中元素的类型。
        [TypeDependency("System.SZArrayHelper")]
        public interface ICollection<T> : IEnumerable<T>, IEnumerable

    原来ICollection<T> 同时继承IEnumerable<T>和IEnumerable两个接口,按我的理解就是,ICollection继续它们2个接口而且扩展了方法,功能强多了。

    由原来的步枪变成半自动步枪

    我们继续看IList:

    public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable

    靠 IList 继承它们三个接口,怪不得功能这么多啊,那应该属于全自动步枪了

    最后来看看List:

    public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

    这个时候大家仔细看看,它们都是接口,只有List 是类,不仅实现它们的接口,而且还扩展了太多的方法给我利用。哇靠,几乎所有功能都能实现了,简直是激光步枪

    按照功能排序:List<T> ----- IList<T> ---- ICollection<T> ---- IEnumerable<T>

    按照性能排序:IEnumerable<T>----ICollection<T>----IList<T>----List<T>

    规律:功能越多,性能越低,所以一般情况下mvc的view页面头部使用IEnumerable<T>的比较多

    那亲们可以根据自身的需求调用不能数组来实现功能,不要一味使用List,谢谢!

  • 相关阅读:
    poj 2187 Beauty Contest(旋转卡壳)
    poj 2540 Hotter Colder(极角计算半平面交)
    poj 1279 Art Gallery(利用极角计算半平面交)
    poj 3384 Feng Shui(半平面交的联机算法)
    poj 1151 Atlantis(矩形面积并)
    zoj 1659 Mobile Phone Coverage(矩形面积并)
    uva 10213 How Many Pieces of Land (欧拉公式计算多面体)
    uva 190 Circle Through Three Points(三点求外心)
    zoj 1280 Intersecting Lines(两直线交点)
    poj 1041 John's trip(欧拉回路)
  • 原文地址:https://www.cnblogs.com/webapi/p/5669058.html
Copyright © 2011-2022 走看看