IEnumerable、ICollection、IList、List之间的区别与方法介绍

区别

如下列出IEnumerable、ICollection、IList、List继承关系。(这里带有泛型,非泛型也是同样的关系)spa

IEnumerable<T>: code

public interface IEnumerable<out T> : IEnumerable

ICollection<T>:blog

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

 IList<T>:排序

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

List<T>:继承

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

功能多少排序:List > IList > ICollection > IEnumerable。接口

 

方法介绍

因为上面的枚举类和接口都有好多方法,因此只介绍下我最近经常使用的。get

IEnumerable:string

一、All<TSource>(Func<TSource, Boolean>) :判斷序列中的全部項目是否全都符合條件。it

class Pet { public string Name { get; set; } public int Age { get; set; } } public static void AllEx() { // Create an array of Pets.
    Pet[] pets = { new Pet { Name="Barley", Age=10 }, new Pet { Name="Boots", Age=4 }, new Pet { Name="Whiskers", Age=6 } }; // Determine whether all pet names // in the array start with 'B'.
    bool allStartWithB = pets.All(pet => pet.Name.StartsWith("B")); Console.WriteLine( "{0} pet names start with 'B'.", allStartWithB ? "All" : "Not all"); }

二、 GetEnumerator:获取序列,以遍历。io

今天先写到这。

相关文章
相关标签/搜索