IEnumerable是集合,IEnumerator是集合的迭代器

我们常用IEnumerable,却忽视IEnumerator。简单来说,IEnumerable是可以被循环遍历的集合,IEnumerator实施循环遍历。

接口分别是:

public interface IEnumerator
{
    bool MoveNext();
    object Current{get;}
    void Reset();
}

public interface IEnumerable
{
    IEnumerator GetEnumerator();
}

来执行这样简单的一个控制台程序。

    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<string> list = new List<string>() {"hello","world"};
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }

在"解决方案资源管理器"中,右键控制台项目,点击"在文件资源管理器中打开文件"。

依次点击"bin"和"Debug"文件夹。

复制下当前文件目录。

打开开发人员命令提示。

由于应用程序文件在F盘,输入如下:

导向到刚才赋值的目录,输入如下:

查看当前目录下的清单,输入如下:

使用.NET的ILDASM反编译应用程序文件,并输入到某个txt文本中。输入如下:

打开1.txt文件,输入如下:

1.txt文件被打开,相关的IL代码如下:

可见,foreach也是语法糖,当使用foreach循环遍历的时候,实际上是调用IEnumerable的IEnumerator的方法。

所以,对于foreach,可以换种写法:

    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<string> list = new List<string>() { "hello", "world" };
            IEnumerator it = list.GetEnumerator();
            while (it.MoveNext())
            {
                Console.WriteLine(it.Current);
            }
            Console.ReadKey();

        }
    }

时间: 2024-08-25 23:24:56

IEnumerable是集合,IEnumerator是集合的迭代器的相关文章

编写高质量代码改善C#程序的157个建议——建议30:使用LINQ取代集合中的比较器和迭代器

建议30:使用LINQ取代集合中的比较器和迭代器 LINQ提供了类似于SQL的语法来实现遍历.筛选与投影集合的功能. static void Main(string[] args) { List<Salary> companySalary = new List<Salary>() { new Salary() { Name = "Mike", BaseSalary = 3000, Bonus = 1000 }, new Salary() { Name = &qu

C# IEnumerable&lt;T&gt;、IEnumerator&lt;T&gt;、List&lt;T&gt;、ArrayList、[]数组各各的区别

List <T>是ArrayList的泛型,ArrayList里边的数据类型是object,List <T>里边的是具体的某种类型. ArrayList类似于向量,可以存储不同的数据类型在一个数组里边(转换为了object). 一般使用的时候尽量使用List <T>,因为ArrayList存取都要进行一次转换(装箱.拆箱). []类型的数组类似于List <T>,不同的是[]是定长的,而List <T>是长度可变的数组. ICollection主

JAVASE02-Unit04: 集合框架 、 集合操作 —— 线性表

Unit04: 集合框架 . 集合操作 -- 线性表 操作集合元素相关方法 package day04; import java.util.ArrayList; import java.util.Collection; import day02.Point; /** * 操作集合元素相关方法 * @author adminitartor * */ public class Collection_Remove { public static void main(String[] args) { C

C#语言基础——集合(ArrayList集合)

集合及特殊集合 集合的基本信息: System.Collections 命名空间包含接口和类,这些接口和类定义各种对象(如列表.队列.位数组.哈希表和字典)的集合.System.Collections.Generic 命名空间包含定义泛型集合的接口和类,泛型集合允许用户创建强类型集合,它能提供比非泛型强类型集合更好的类型安全性和性能.ystem.Collections.Specialized 命名空间包含专用的和强类型的集合,例如,链接的列表词典.位向量以及只包含字符串的集合. 常用的集合为Ar

有一个集合,判断集合里有没有“world”这个元素,如果有,添加“javaee”

// 有一个集合,判断集合里有没有"world"这个元素,如果有,添加"javaee" List list = new ArrayList(); list.add("world"); list.add("java"); list.add("hello"); //ConcurrentModificationException /*Iterator it = list.iterator(); while(it.

2016年10月16日--ArrayList集合、特殊集合

ArrayList集合 使用前引用 using System.Collections; ArrayList集合 实例化.初始化 ArrayList al = new ArrayList(); ArrayList与Array的名字很相似. 相同点: (1)两者都实现了IList.ICollection.IEnumerable接口: (2)两者都可以使用整数索引访问集合中的元素,包括读取和赋值,且集合中的索引都从0开始. 不同点: (1)ArrayList是集合,而Array是数组: (2)Arra

(2)集合 遍历set集合

set集合的一些方法 Set<String> set1=new HashSet<String>(); set1.add("a"); set1.add("b"); set1.add("c"); set1.add("d"); Set<String> set2=new HashSet<String>(); set2.add("a"); set2.add("

IEnumerable接口与IEnumerator接口

通过一个例子来看 -------------------------------------------------------Student.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication6 {  

java学习笔记—集合之Map集合

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; text-align: center; font: 12.0px Times } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Songti SC" } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Times } p.p4 { margin: 0.0px 0.0px 0.0px 0.0