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是 ...
随机推荐
- optimize PHP-FPM优化
php-fpm进程pidpids=$(ps aux | grep ${process} | grep -v "grep" | awk '{print $2}') php-fpm 关 ...
- luogu3674 小清新人渣的本愿 (bitset+莫队)
对于加减,用bitset维护当前每个数有没有 对于乘,暴力枚举约数 然后莫队 复杂度$O(m(\sqrt{n}+\frac{c}{64}))$ #include<bits/stdc++.h> ...
- A1094. The Largest Generation
A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level bel ...
- netsh interface portproxy的一个简单例子
netsh interface portproxy的微软帮助文档地址: https://technet.microsoft.com/zh-cn/library/cc776297(WS.10).aspx ...
- 第七篇-列表式App:ListActivity及ListView
一.新建一个empty activity的项目. 二.修改MainActivity.java: extends AppCompactActivity改为extends ListActivity.注释掉 ...
- TestNg 10. 多线程测试-xml文件实现
代码如下: package com.course.testng.multiThread; import org.testng.annotations.Test; public class MultiT ...
- android studio adb.exe已停止工作(全面成功版 进程的查询和开启)
先输入adb看是否存在. 如果不存在则:在系统path里添加C:\Users\nubia\AppData\Local\Android\sdk\platform-tools 因为这个目录里有adb 或者 ...
- xp与win7双系统时删除win7启动菜单
1.提取Win7安装光盘中,boot目录下的[bootsect.exe]到其它盘中,备用! 2 2.启动到WinXP或者WinPE或者Dos中,只要是能执行dos命令的地方就可以 3 3.cmd下进入 ...
- bind,apply,call,caller,callee还傻傻分不清楚?
先介绍每个的语法: 1. bind() 语法:fn.bind(thisObj[, arg1[, arg2[, ...]]]) fn:是想要改变this指向的函数 thisObj:表示fn中this指针 ...
- Code review应该怎么做
代码评审有两种不同的方法,一种是代码走查,一种是代码审查,我们这里讨论的仅指代码走查.通常自己写的代码都难以发现问题,需要以第二双眼睛再次检查代码,帮助我们及时地发现潜在的问题. 做代码审查之前,团队 ...