C#--IEnumerable 与 IEnumerator 的区别
一、 IEnumerator
解释:它是一个的集合访问器,使用foreach语句遍历集合或数组时,就是调用 Current、MoveNext()的结果。
- // 定义如下
public interface IEnumerator- {
- // 返回结果: 集合中的当前元素。
- object Current { get; }
- // 返回结果: 如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
- bool MoveNext();
- // 调用结果:将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
- void Reset();
- }
二、IEnumerable
解释:它利用 GetEnumerator() 返回 IEnumerator 集合访问器。
- // 定义如下
- public interface IEnumerable
- {
- // 返回结果: 可用于循环访问集合的IEnumerator 对象。
- IEnumerator GetEnumerator();
- }
三、举个栗子
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace ArrayListToList
- {
- // 定义student类
- public class Student
- {
- public string Id { get; set; }
- public string Name { get; set; }
- public string Remarks { get; set; }
- public Student(string id, string name, string remarks)
- {
- this.Id = id;
- this.Name = name;
- this.Remarks = remarks;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ArrayList arrStus = new ArrayList
- {
- new Student("", "liuliu"," little rabbit"),
- new Student("", "zhangsan", "little tortoise")
- };
- // List<T> 继承了IEnumerable<T>, IEnumerble<T>继承了IEnumerable.
- List<Student> stuL = ArrListToArr<Student>(arrStus);
- foreach(Student stu in stuL)
- {
- Console.WriteLine($"{ stu.Name + " " + stu.Id + " " + stu.Remarks }");
- };
- }
- // arrList 转换为 List<T>
- // ArrList 定义时已继承了IEnumerable
- static List<T> ArrListToArr<T>(ArrayList arrL)
- {
- List<T> list = new List<T>();
- IEnumerator enumerator = arrL.GetEnumerator();
- while (enumerator.MoveNext())
- {
- T item = (T)(enumerator.Current);
- list.Add(item);
- }
- return list;
- }
- }
- }
结果:
C#--IEnumerable 与 IEnumerator 的区别的更多相关文章
- 关于迭代器中IEnumerable与IEnumerator的区别
首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumer ...
- 转载IEnumerable与IEnumerator区别
public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { ...
- C#编程之IList<T>、List<T>、ArrayList、IList, ICollection、IEnumerable、IEnumerator、IQueryable 和 IEnumerable的区别
额...今天看了半天Ilist<T>和List<T>的区别,然后惊奇的发现使用IList<T>还是List<T>对我的项目来说没有区别... 在C#中 ...
- Asp.Net IEnumerable,ICollection,IList,List区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...
- 在C#中IEnumerable与IEnumerator
对于很多刚开始学习C#同学来说经常会遇到IEnumerable这个关键字,enumerate在字典里的解释是列举,枚举,因此可想而知这个关键字肯定是和列举数据有关的操作. public interfa ...
- IEnumerable,ICollection,IList,List区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...
- C#中的 IList, ICollection ,IEnumerable 和 IEnumerator
IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...
- IList, ICollection ,IEnumerable AND IEnumerator in C#
IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...
- 由IEnumerable和IEnumerator的延伸
相信大家在学习c#的时候,经常会看到IEnumerable.IEnumerator这样的接口或返回类型,在写代码时经常会对数组或List集合进行遍历.那IEnumerable和IEnumerator是 ...
随机推荐
- 「SCOI2015」小凸玩矩阵 解题报告
「SCOI2015」小凸玩矩阵 我好沙茶啊 把点当边连接行和列,在外面二分答案跑图的匹配就行了 我最开始二分方向搞反了,样例没过. 脑袋一抽,这绝壁要费用流,连忙打了个KM 然后wa了,一想这个不是完 ...
- Uva796 Critical Links
用tarjan缩点 然后用dfn[u] < low[v]缩点并且保存起来 在sort一遍输出 #include<stdio.h> #include<string.h> # ...
- css 圆形头像
方法一:背景图片(推荐) 好处是,图片长宽不等的情况下图片不会变形 .ui-photo { width: 100px; height: 100px; background: url("img ...
- HEOI2019退役总结
真的很快,一切就都已经尘埃落定了. 其实经历不是很圆满的时候,是不想写这一类游记总结的,但这次其实不太一样,总要让这段经历有始有终. 可能会很啰嗦…… 赛前 收到了若干鼓励,包括老师的手写祝福和学长学 ...
- CSS解决文字超出显示省略号问题
超出一行 white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 超出多行 overflow: hidden; text-ove ...
- A1072. Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
- Django 路由报错友好提示
这个方法要在设置路由文件内使用也就是urls.py内. """mysite URL Configuration The `urlpatterns` list routes ...
- Django(十九)Ajax全套
参考博客:http://www.cnblogs.com/wupeiqi/articles/5703697.html 提交: - Form - Ajax 一.Ajax,偷偷向后台发请求 - XMLHtt ...
- redis4.0.6集群搭建
文件环境:CentOS7 + redis4.0.6 先去官网下载redis:https://redis.io/,然后上传到你的虚拟机,我上传到了/mysoft 先解压->然后进入主目录-> ...
- Good Bye 2018
Good Bye 2018 2018年最后一场CF,OVER! 弱弱的我只能做出3道A,B,D~~~~ 最后几分钟,感觉找到了C题的规律,结束的那一刻,提交了一发 "Wrong answer ...