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是 ...
随机推荐
- 解决SSH连接出现 Software caused connection abort 的问题
修改服务器中/etc/ssh/sshd.config 文件,将LoginGraceTime的值设为0,默认为2m,TCPKeepAlive 设为yes, 然后使用service sshd restar ...
- Python3 与 C# 并发编程之~进程先导篇
在线预览:http://github.lesschina.com/python/base/concurrency/1.并发编程-进程先导篇.html Python3 与 C# 并发编程之- 进程篇 ...
- POJ--3190 Stall Reservations(贪心排序)
这里 3190 Stall Reservations 按照吃草时间排序 之后我们用 优先队列维护一个结束时间 每次比较堆顶 看是否满足 满足更新后放到里面不满足就在后面添加 #include<c ...
- GWAS研究可利用的数据库(持续更新)
1.列表包括数据库名称.表型.是否能下载到基因型(genotype).是否能下载到GWAS结果文件(P值.效应值.SNP位点).目前收集到的有如下: 参考到这些数据库的文献:Genome-wide a ...
- tail 命令只查看日志中的关键字所在行信息
tail -f info_log-2019-04-20.log |grep 要查询的关键字
- HDU 4770 Lights Against Dudely(暴力+状压)
思路: 这个题完全就是暴力的,就是代码长了一点. 用到了状压,因为之前不知道状压是个东西,大佬们天天说,可是我又没学过,所以对状压有一点阴影,不过这题中的状压还是蛮简单的. 枚举所有情况,取开灯数最少 ...
- 异步请求 ajax的使用详解
https://blog.csdn.net/happyaliceyu/article/details/52381446 可以说是很详细了,赞
- node基础(二)_模块以及处理乱码问题
一.前言 本次内容主要包括: 1.node.js中的模块系统 2.解决上篇中服务器响应的汉字乱码问题 二.知识 1.node中的模块 分为三种: 核心模块(node定义的如前面用到的fs,http ...
- 根据指定的key,将二维数组的value转换为string,适用于mysql的in查询
function array_unique_join($arr,$param){ $utm_source_arr = array_unique(array_column($arr,$param)); ...
- django补充
通过表名获取app的name models.UserInfo._meta.app_label >>> from repository import models >>&g ...