关于迭代器中IEnumerable与IEnumerator的区别
首先是IEnumerable与IEnumerator的定义:
1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项。
2.IEnumerator接口是一个真正的集合访问器,它包含MoveNext()方法和Current属性,在foreach循环中,如果MoveNext()返回True,则就是用IEnumerator接口的Current属性来获取对象的一个引用,用于foreach循环。
3.如果要迭代一个类,可以使用GetEnumerator(),其返回类型是IEnumerator.
如果要迭代一个类成员,则用IEnumerable.
下面的例子是迭代Person类中的类成员Ages,使用了IEnumerable。第二个例子则是迭代一个类,所以使用了IEnumerator作为返回值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; namespace _10_5_5
{
public class person
{
private string name;
private int age;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public int Age
{
get
{
return age;
}
set
{
age = value;
}
}
public person(string PName, int PAge)
{
Name = PName;
Age = PAge;
}
public static bool operator >(person a, person b)
{
if (a.Age > b.Age)
return true;
else
return false;
}
public static bool operator <(person a, person b)
{
if (a.Age > b.Age)
return false;
else
return true;
}
public static bool operator >=(person a, person b)
{
if (a.Age >= b.Age)
{
return true;
}
else
return false;
}
public static bool operator <=(person a, person b)
{
if (a.Age <= b.Age)
return true;
else
return false;
}
}
public class People : DictionaryBase
{
public IEnumerable Ages//注意是IEnumerable
{
get
{
foreach (object person in Dictionary.Values)
{
yield return (person as person).Age;
}
}
}
public person[] GetOldest()
{
People oldPeople = new People();
person oldPerson = null;
person currentPerson;
foreach (DictionaryEntry myPeople in Dictionary)
{
currentPerson = myPeople.Value as person;
if (oldPerson == null)
{
oldPerson = currentPerson;
oldPeople.Add(oldPerson);
}
else
{
if (currentPerson > oldPerson)
{
oldPeople.Clear();
oldPeople.Add(currentPerson);
oldPerson = currentPerson;
}
else
{
if (currentPerson >= oldPerson)
{
oldPeople.Add(currentPerson);
}
}
}
}
person[] oldestPeopleArray = new person[oldPeople.Count];
int copyIndex = ;
foreach (DictionaryEntry p in oldPeople)
{
oldestPeopleArray[copyIndex] = p.Value as person;
copyIndex++;
}
return oldestPeopleArray;
}
public void Add(person p)
{
Dictionary.Add(p.Name, p);
}
public person this[string SName]
{
get
{
return (person)Dictionary[SName];
}
set
{
Dictionary[SName] = value;
}
} }
class Program
{
static void Main(string[] args)
{
person a = new person("Jack", );
person b = new person("Json", );
People s = new People();
s.Add(a);
s.Add(b);
foreach(int age in s.Ages)
{
Console.WriteLine("{0}\t", age);
}
Console.ReadKey();
}
}
}
下面是自定义的一个迭代器的例子:
Primer.CS
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Ch11Ex03_Exam
{
public class Primes
{
private long min;
private long max;
public Primes():this(,)
{ }
public Primes(long minNum,long maxNum)
{
if(minNum<)
{
min=;
}else{
min = minNum;
}
max = maxNum;
}
public IEnumerator GetEnumerator()//返回的是IEnumerator
{
for(long i=min;i<max;i++)
{
int flag = ;
for(long j=;j<Math.Sqrt(min);j++)
{
if(i%j==)
{
flag = ;
break;
}
}
if(flag==)
{
yield return i;
}
}
}
}
}
Program.CS:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Ch11Ex03_Exam
{
class Program
{
static void Main(string[] args)
{
Primes s = new Primes(, );
foreach(long i in s)
{
Console.WriteLine("{0}\t", i);
}
Console.ReadKey();
}
}
}
关于迭代器中IEnumerable与IEnumerator的区别的更多相关文章
- 在C#中IEnumerable与IEnumerator
对于很多刚开始学习C#同学来说经常会遇到IEnumerable这个关键字,enumerate在字典里的解释是列举,枚举,因此可想而知这个关键字肯定是和列举数据有关的操作. public interfa ...
- C#中IEnumerable和IEnumerator区别
IEnumerator:是一个真正的集合访问器,提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型.IEnumerable: ...
- C#--IEnumerable 与 IEnumerator 的区别
一. IEnumerator 解释:它是一个的集合访问器,使用foreach语句遍历集合或数组时,就是调用 Current.MoveNext()的结果. // 定义如下public interface ...
- C# ~ 从 IEnumerable / IEnumerator 到 IEnumerable<T> / IEnumerator<T> 到 yield
IEnumerable / IEnumerator 首先,IEnumerable / IEnumerator 接口定义如下: public interface IEnumerable /// 可枚举接 ...
- C#中的 IList, ICollection ,IEnumerable 和 IEnumerator
IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...
- C#编程之IList<T>、List<T>、ArrayList、IList, ICollection、IEnumerable、IEnumerator、IQueryable 和 IEnumerable的区别
额...今天看了半天Ilist<T>和List<T>的区别,然后惊奇的发现使用IList<T>还是List<T>对我的项目来说没有区别... 在C#中 ...
- 细说 C# 中的 IEnumerable和IEnumerator接口
我们先思考几个问题: 为什么在foreach中不能修改item的值? 要实现foreach需要满足什么条件? 为什么Linq to Object中要返回IEnumerable? 接下来,先开始我们的正 ...
- 转载IEnumerable与IEnumerator区别
public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { ...
- C#中IEnumerable、ICollection、IList、IQueryable 、IQueryable 、List之间的区别
一:一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Reset ...
随机推荐
- LCLFramework框架之数据门户
LCLFramework框架之数据门户职责 形成统一的数据访问方式. LCLFramework框架之数据门户设计 数据门户分为客户端/服务端. LCLFramework框架之数据门户设计代码 数 ...
- 连接的世界 - LTE时代产业趋势和战略分析
连接的世界 - LTE时代产业趋势和战略分析 作者:华为有线技术公司李常伟 2014-09-22 信息产业发展解放的核心是这个世界连接的方式.由语音到数据.由通信到情感.由人的连接到物的连接.由“哑” ...
- [Compose] 21. Apply Natural Transformations in everyday work
We see three varied examples of where natural transformations come in handy. const Right = x => ( ...
- c#如何区分静态只读变量和常量
常量const 常量就是一个其值永远不会改变的静态字段.常量的值会在编译时自动推算,编译器会在遇到常量时,将其逐个替换为该常量的值.常量可以是C#内建的任何数字类型或枚举类型.声明一个常量的时候必须对 ...
- iOS开发——程序员必备&iOS安装包的三种格式 deb、ipa 和 pxl的解释和说明
iOS安装包的三种格式 deb.ipa 和 pxl的解释和说明 目前 iOS 平台上常见的安装包有三种,deb.ipa 和 pxl.转自链接:http://fanlb.blogbus.com/logs ...
- Intent用法简介
Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自动调用合适的程序打开不同类型的 ...
- X509证书中RSA公钥的提取与载入
原文链接: http://blog.chinaunix.net/uid-16515626-id-2741894.html 由于项目需要,我计划利用openssl开发一个基本的CA,实现证书的发放等 ...
- Spring3系列4-多个配置文件的整合
Spring3系列4-多个配置文件的整合 在大型的Spring3项目中,所有的Bean配置在一个配置文件中不易管理,也不利于团队开发,通常在开发过程中,我们会按照功能模块的不同,或者开发人员的不同,将 ...
- Spark源码系列(一)spark-submit提交作业过程
前言 折腾了很久,终于开始学习Spark的源码了,第一篇我打算讲一下Spark作业的提交过程. 这个是Spark的App运行图,它通过一个Driver来和集群通信,集群负责作业的分配.今天我要讲的是如 ...
- linux标准daemon编写方式
daemon定义 运行在后台的程序,通常不需要与用户进行交互的. 任何父进程id是0的通常是kernel进程,作为系统启动的一部分,除了init是用户态的命令. 规则 第一件事情是调用umask设置文 ...