一、 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 的区别的更多相关文章

  1. 关于迭代器中IEnumerable与IEnumerator的区别

    首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumer ...

  2. 转载IEnumerable与IEnumerator区别

    public interface IEnumerable {     IEnumerator GetEnumerator(); }   public interface IEnumerator {   ...

  3. C#编程之IList<T>、List<T>、ArrayList、IList, ICollection、IEnumerable、IEnumerator、IQueryable 和 IEnumerable的区别

    额...今天看了半天Ilist<T>和List<T>的区别,然后惊奇的发现使用IList<T>还是List<T>对我的项目来说没有区别...  在C#中 ...

  4. Asp.Net IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  5. 在C#中IEnumerable与IEnumerator

    对于很多刚开始学习C#同学来说经常会遇到IEnumerable这个关键字,enumerate在字典里的解释是列举,枚举,因此可想而知这个关键字肯定是和列举数据有关的操作. public interfa ...

  6. IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  7. C#中的 IList, ICollection ,IEnumerable 和 IEnumerator

    IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...

  8. IList, ICollection ,IEnumerable AND IEnumerator in C#

    IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...

  9. 由IEnumerable和IEnumerator的延伸

    相信大家在学习c#的时候,经常会看到IEnumerable.IEnumerator这样的接口或返回类型,在写代码时经常会对数组或List集合进行遍历.那IEnumerable和IEnumerator是 ...

随机推荐

  1. NOIp2018 游记

    作为一名蒟蒻,对于NOIp当然是不抱什么希望.所以就只能在比赛中吸取经验咯... Day0 害怕书到用时方恨少,疯狂打板子(玩电脑) Day1 来到考场了,发现键盘空格按不起,觉得非常尴尬,然后他告诉 ...

  2. 【mysql】mysql常用语句

    返回不重复数据 SELECT DISTINCT user_name,vistor_username FROM KY_FEED_VISTOR WHERE user_name='shenhy' 单独的di ...

  3. centos7破解安装fisheye和Crucible

    背景介绍: Atlassian的东西相信大家都不陌生,JIRA.Confluence……虽然说这些产品都要收费,也可以申请试用: FishEye 可以方便地查看代码,而Crucible 则是进行Cod ...

  4. Keepalived+Nginx搭建主从高可用并带nginx检测

    应用环境:部分时候,WEB访问量一般,或者测试使用,利用Keepalived给Nginx做高可用即可满足要求. 测试环境:   搭建步骤: 1. 安装软件 在Nginx-A和Nginx-B上: ~]# ...

  5. codeforces #530 D(Sum in the tree) (树上贪心)

    Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each ve ...

  6. COGS 2392 2393 2395 有标号的二分图计数

    有黑白关系: 枚举左部点(黑色点),然后$2^{i*(n-i)}$处理方法同:COGS 2353 2355 2356 2358 有标号的DAG计数 无关系: 发现,假设$f(i)$是一个连通块,对于一 ...

  7. A1127. ZigZagging on a Tree

    Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can ...

  8. 利用selenium并使用gevent爬取动态网页数据

    首先要下载相应的库 gevent协程库:pip install gevent selenium模拟浏览器访问库:pip install selenium selenium库相应驱动配置  https: ...

  9. (六)Oracle 的 oracle表查询关键字

    参考:http://www.hechaku.com/Oracle/oracle_tables2.html 1.使用逻辑操作符号问题:查询工资高于500或者是岗位为manager的雇员,同时还要满足他们 ...

  10. 开发问题及解决--java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout

    <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android=" ...