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

本文详细介绍了C#中IEnumerable、ICollection、IList及List之间的继承关系,并通过实例演示了IEnumerable部分方法如All的使用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

区别

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

IEnumerable<T>: 

public interface IEnumerable<out T> : IEnumerable

ICollection<T>:

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。

 

方法介绍

由于上面的枚举类和接口都有好多方法,所以只介绍下我最近常用的。

IEnumerable:

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

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");
}

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

今天先写到这。

转载于:https://www.cnblogs.com/bibi-feiniaoyuan/p/9214657.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值