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是 ...
随机推荐
- 「NOI2016」优秀的拆分 解题报告
「NOI2016」优秀的拆分 这不是个SAM题,只是个LCP题目 95分的Hash很简单,枚举每个点为开头和末尾的AA串个数,然后乘一下之类的. 考虑怎么快速求"每个点为开头和末尾的AA串个 ...
- Mysql 系统表
Information_schema: 1. tables 提供表信息: 表所属数据库,表名,表类型,行数,最大自增数等等.
- centos7修改默认网卡名称
问题场景: 使用centos7有好一阵子了,安装过centos7的朋友都会发现网卡命名跟6.x系统的不一样,类似ifcfg-eno16780032, ens192,或者enp2s0等其他不习惯的.不容 ...
- Building Microservices with Spring Boot and Apache Thrift. Part 1 with servlet
https://dzone.com/articles/building-microservices-spring In the modern world of microservices it's i ...
- ftp 两台服务器传输文件 apache
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.OutputS ...
- js 调试技巧
快捷键 1.快速查看HTML中dom元素绑定那些JS事件方法 chrome中 F12-->Elements-->Event Listenners 参考:https://jingy ...
- [luogu1970][花匠]
题目地址 https://www.luogu.org/problemnew/show/P1970 题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部 ...
- MVC之 自定义过滤器(ActionFilterAttribute)
一.自定义Filter 自定义Filter需要继承ActionFilterAttribute抽象类,重写其中需要的方法,来看下ActionFilterAttribute类的方法签名. //表示所有操作 ...
- Good Bye 2018 A. New Year and the Christmas Ornament
传送门 https://www.cnblogs.com/violet-acmer/p/10201535.html 题解: 这题没什么好说的,读懂题意就会了. 比赛代码: #include<ios ...
- appium在不同类中使用的是同一个session
要这么做的起因: 测试testng框架的时候,不同类之间可以按照顺序执行,不会互相干扰.但是换成了appium,在A类中启动了session,初始化或者一些数据我仍然要用之前的session,那就不行 ...