IEnumerable、IEnumerator、ICollection、IList、List的继承关系及简单使用

IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单的迭代,IEnumerable和IEnumerable<T>接口是.NET Framework中最基本的集合访问器。它定义了一组扩展方法,用来对数据集合中的元素进行遍历、过滤、排序、搜索等操作。

IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。IEnumerator对象有什么呢?它是一个真正的集合访器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。

1、IEnumerator接口

提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型。

IEnumerator<T>:继承自IEnumerator,有Current属性,返回的是T类型。

1     public interface IEnumerator
2     {
3       bool MoveNext(); //将游标的内部位置向前移动
4       object Current{get;} //获取当前的项(只读属性)
5       void Reset(); //将游标重置到第一个成员前面
6     }

2、IEnumerable接口

暴露一个IEnumerator,支持在普通集合中的遍历。

IEnumerable<T>:继承自IEnumerable,暴露一个IEnumerator<T>,支持在泛型集合中遍历。

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

可以看到,GetEnumerator方法返回对另一个接口System.Collections.IEnumerator的引用。这个接口提供了基础设施,调用方可以用来移动IEnumerable兼容容器包含的内部对象。

3、ICollection接口

同时继承IEnumerable<T>和IEnumerable两个接口

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

4、IList接口

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

5、List的定义

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

实现IEnumerable接口

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Collections;
 6 namespace IEnumeratorSample
 7 {
 8     class Person : IEnumerable
 9     {
10         public string Name;//定义Person的名字
11         public string Age;//定义Person的年龄
12 
13         public Person(string name, string age)//为Person初始化(构造函数)
14         {
15             Name = name;
16             Age = age;
17         }
18 
19         private Person[] per;
20 
21         public Person(Person[] array)//重载构造函数,迭代对象
22         {
23             per = new Person[array.Length];//创建对象
24             for (int i = 0; i < array.Length; i++)//遍历初始化对象
25             {
26                 per[i] = array[i];//数组赋值
27             }
28         }
29 
30         public IEnumerator GetEnumerator()//实现接口
31         {
32             return new PersonEnum(per);
33         }
34     }
35 
36     class PersonEnum : IEnumerator//实现foreach语句内部,并派生
37     {
38         public Person[] _per;//实现数组
39         int position = -1;//设置“指针”
40 
41         public PersonEnum(Person[] list)
42         {
43             _per = list;//实现list
44         }
45 
46         public bool MoveNext()//实现向前移动
47         {
48             position++;//位置增加
49             return (position < _per.Length);//返回布尔值
50         }
51 
52         public void Reset()//位置重置
53         {
54             position = -1;//重置指针为-1
55         }
56 
57         public object Current//实现接口方法
58         {
59             get
60             {
61                 try
62                 {
63                     return _per[position];//返回对象
64                 }
65                 catch (IndexOutOfRangeException)//捕获异常
66                 {
67                     throw new InvalidOperationException();//抛出异常信息
68                 }
69             }
70         }
71     }
72 
73     class Program
74     {
75         static void Main(string[] args)
76         {
77             Person[] per = new Person[2]
78             {
79                 new Person("Jack","21"),
80                 new Person("David","24"),
81             };
82             Person personlist = new Person(per);
83             //遍历对象
84             foreach (Person p in personlist)
85             {
86                 Console.WriteLine("Name is " + p.Name + " and Age is " + p.Age);
87             }
88             Console.ReadKey();
89         }
90     }
91 }

原文链接:http://www.studyofnet.com/news/452.html

相关博客1:http://blog.csdn.net/callmeback/article/details/8295648

相关博客2:http://www.cnblogs.com/shaosks/archive/2011/09/27/2193270.html

转载于:https://www.cnblogs.com/makesense/p/6308554.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值