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

 public interface IEnumerable {       //IEnumerable只有一个方法,返回可循环访问集合的枚举数。
IEnumerator GetEnumerator()
;
} public interface IEnumerator { // 方法
//移到集合的下一个元素。如果成功则返回为 true;如果超过集合结尾,则返回false。
bool MoveNext();
// 将集合设置为初始位置,该位置位于集合中第一个元素之前
void Reset();
// 属性:获取集合中的当前元素
object Current { get; }
}

看我们手动自定义的Student类,以及Student1,StudentEnum类(分别继承了IEnumerable,IEnumerator接口),上代码

 using System;
using System.Collections;
using System.Text;
using System.Threading.Tasks; namespace Enumerate
{
public class Student
{ public string Name;
public int Score;
public Student(string name, int score)
{ this.Name = name;
this.Score = score;
}
} public class Student1: IEnumerable { public Student[] students;
public Student1(Student[] stArray)
{
students = new Student[stArray.Length];
for (int i = ; i < stArray.Length; i++)
{
students[i] = stArray[i];
}
}
public IEnumerator GetEnumerator()
{
//throw new NotImplementedException();
return new StudentEnum(students);
}
} public class StudentEnum : IEnumerator
{
public Student[] students; int position = -; public StudentEnum(Student[] studentlist)
{ students = studentlist;
} public object Current
{
//get { throw new NotImplementedException(); }
get { try
{
return students[position];
}
catch (Exception ex)
{ return ex.Message;
}
}
} public bool MoveNext()
{
//throw new NotImplementedException();
position++;
return position >= students.Length ? false : true;
} public void Reset()
{
//throw new NotImplementedException();
position = -;
}
} class Program
{
static void Main(string[] args)
{
Student[] studentArrary = new Student[] { new Student("test1",),
new Student("test2",),
};
Student1 studenlist = new Student1(studentArrary);
foreach (Student item in studenlist)
{ Console.WriteLine(item.Name +" "+ item.Score); }
Console.Read();
}
}
}

IEnumerable和IEnumerator有什么区别?

1、一个Collection要支持foreach方式的遍历,必须实现IEnumerable接口(亦即,必须以某种方式返回IEnumerator object)。
 
2、IEnumerator object具体实现了iterator(通过MoveNext(),Reset(),Current)。
 
3、从这两个接口的用词选择上,也可以看出其不同:IEnumerable是一个声明式的接口,声明实现该接口的class是“可枚举(enumerable)”的,但并没有说明如何实现枚举器(iterator);IEnumerator是一个实现式的接口,IEnumerator object就是一个iterator。
 
4、IEnumerable和IEnumerator通过IEnumerable的GetEnumerator()方法建立了连接,client可以通过IEnumerable的GetEnumerator()得到IEnumerator object,在这个意义上,将GetEnumerator()看作IEnumerator object的传递方法。

在C#中IEnumerable与IEnumerator的更多相关文章

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

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

  2. C#中IEnumerable和IEnumerator区别

    IEnumerator:是一个真正的集合访问器,提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型.IEnumerable: ...

  3. 细说 C# 中的 IEnumerable和IEnumerator接口

    我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...

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

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

  5. 迭代器学习之一:使用IEnumerable和IEnumerator接口

    写博客是检验我学习的成果之一以及自我总结的一种方式,以后会经常利用这种方式进行技术交流和自我总结,其中认识不深难免会有错误,但是一直懂得不懂就问,不懂就学的道理! 1.首先看一个简单的列子 , , , ...

  6. C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield

    IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...

  7. IEnumerable和IEnumerator

    概述 IEnumerable和IEnumerator接口存在的意义:用来实现迭代的功能! public interface IEnumerable { IEnumerator GetEnumerato ...

  8. IEnumerable和IEnumerator 详解 (转)

    原文链接:http://blog.csdn.net/byondocean/article/details/6871881 参考链接:http://www.cnblogs.com/hsapphire/a ...

  9. C#基础知识系列九(对IEnumerable和IEnumerator接口的糊涂认识)

    前言 IEnumerable.IEnumerator到现在为止对这两个接口还是不太理解,不理解但是自己总是想着试着要搞明白,毕竟自己用的少,所以在此先记录一下.以备自己日后可以来翻查,同时也希望园子里 ...

随机推荐

  1. 文件I/O(不带缓冲)之write函数

    调用write函数向打开的文件写数据. #include <unistd.h> ssize_t write( int filedes, const void *buf, size_t nb ...

  2. com.domain.bean

    package com.domain.bean; import java.util.LinkedHashMap; import java.util.Map; public class TmpBean ...

  3. Spring(AbstractRoutingDataSource)实现动态数据源切换--转载

    原始出处:http://linhongyu.blog.51cto.com/6373370/1615895 一.前言 近期一项目A需实现数据同步到另一项目B数据库中,在不改变B项目的情况下,只好选择项目 ...

  4. 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

  5. Android_gridVIew

    xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  6. .Net 把网页Html转PDF文件

    .Net 把网页Html转PDF文件 此篇主要利用 wkhtmltopdf  进行转换. 一.控制台直接转换 首先到官网http://wkhtmltopdf.org/下载wkhtmltopdf  ,下 ...

  7. webform处理过程

    一.post/get传值注意几点 post提交的时候,只有写了name属性且没有写disable=true表单元素(input,select,textarea)才会被提交. 如果不确定是get还是po ...

  8. ios registerNib: and registerClass:

    先看看apple官网简述: registerNib:forCellWithReuseIdentifier: Register a nib file for use in creating new co ...

  9. ###使用phpmailer自动邮件提醒

    近期的一个小项目中需要用到邮件自动通知功能,搜了搜,发现phpmailer这个东西大家用的挺多的. 首先去sourceforge下载phpmailer,也可去我的网盘下载,我用的是5.1版本的. 看到 ...

  10. ubuntu 恢复gnome-panel

    Ubuntu重启panel 的方法首先进入终端, 依次输入以下命令1.gconftool --recursive-unset /apps/panel2.rm -rf ~/.gconf/apps/pan ...