概念文字性的东西,我们就不说了,这里我们来点具体的实例第呀;

实例一:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Pratice001
{
public class Test:IEnumerable
{
private static int count = ;
private string[] elements;
private int ctr = ; //数组下标的计数器; public Test(params string[] initialString)
{
elements = new String[];
foreach (string s in initialString)
{
elements[ctr++] = s; //数据初始化
}
}
public Test(string initialString, char[] delimiters)
{
elements = initialString.Split(delimiters); //不同的方式 进行初始化;
} //实现接口中的方法;
public IEnumerator GetEnumerator()
{
return new ForeachTestEnumerator(this);
} public class ForeachTestEnumerator : IEnumerator
{
private int position = -;
private Test t;
public ForeachTestEnumerator(Test t)
{
this.t = t;
} //实现接口;
public object Current
{
get
{
return t.elements[position];
}
} public bool MoveNext()
{
if (position < t.elements.Length - )
{
position++;
return true;
}
else
{
return false;
}
} public void Reset()
{
position = -;
} } } }

实例二:

然后我们再来一个实例!

using System;
using System.Collections; public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
} public string firstName;
public string lastName;
} public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length]; for (int i = ; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
} IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator) GetEnumerator();
} public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
} public class PeopleEnum : IEnumerator
{
public Person[] _people; // Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -; public PeopleEnum(Person[] list)
{
_people = list;
} public bool MoveNext()
{
position++;
return (position < _people.Length);
} public void Reset()
{
position = -;
} object IEnumerator.Current
{
get
{
return Current;
}
} public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
} class App
{
static void Main()
{
Person[] peopleArray = new Person[]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
}; People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName); }
}

最好,跟代码,一步步的调试出来看看的呀;

IEnumerable、GetEnumerator、IEnumerator的理解的更多相关文章

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

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

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

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

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

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

  4. [转]那些年我还不懂:IList,ICollection,IEnumerable,IEnumerator,IQueryable

    1.首先看一个简单的例子 int[] myArray = { 1, 32, 43, 343 }; IEnumerator myie = myArray.GetEnumerator(); myie.Re ...

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

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

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

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

  7. 由IEnumerable和IEnumerator的延伸

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

  8. C# IEnumerable 和 IEnumerator接口浅析

    温故而知新,可以为师矣,有空经常复习一下基础知识是有必要的,并且能加深理解和记忆. Foreach常用于循环访问集合,对实现IEnumerable的接口的容器进行遍历,IEnumerable和IEnu ...

  9. 15.IEnumerable和IEnumerator

    先说IEnumerable,我们每天用的foreach你真的懂它吗? 阅读目录 自己实现迭代器 yield的使用 怎样高性能的随机取IEnumerable中的值 我们先思考几个问题: 为什么在fore ...

随机推荐

  1. BZOJ2981 : [Poi2002]括号

    对于最终加入了括号的序列,对其求中缀表达式,建树. 可以发现$n-1$个运算符DFS序递增,且若一个-上方往左走了奇数次,则它就是+,否则就是-. 所以考虑DP,设$f[i][j]$表示考虑了前$i$ ...

  2. js函数中参数的传递

    数据类型 在 javascript 中数据类型可以分为两类: 基本类型值 primitive type,比如Undefined,Null,Boolean,Number,String. 引用类型值,也就 ...

  3. XCOJ 1103 (LCA+树链最大子段和)

    题目链接: http://xcacm.hfut.edu.cn/problem.php?id=1103 题目大意:链更新.链查询,求树链的最大子段和.(子段可以为空) 解题思路: 将所有Query离线存 ...

  4. 限制Boltzmann机(Restricted Boltzmann Machine)

    起源:Boltzmann神经网络 Boltzmann神经网络的结构是由Hopfield递归神经网络改良过来的,Hopfield中引入了统计物理学的能量函数的概念. 即,cost函数由统计物理学的能量函 ...

  5. 关于jsp乱码问题的产生原因 及 解决方法。

    http://blog.csdn.net/caoxiaohong/article/details/1781777 JSP/JDBC MySQL乱码问题JSP的request 默认为ISO8859_1, ...

  6. Android ListView item项 显示动画

    (1)使用LayoutAnimation 所谓的布局动画,其实就是为ViewGroup添加显示动画效果,主要用过LayoutAnimationController来控制实现.LayoutAnimati ...

  7. 【BZOJ】1532: [POI2005]Kos-Dicing

    题意 \(n\)个人\(m\)场比赛\((1 \le n \le 10000, 0 \le m \le 10000)\),给出每场比赛的两个选手,求赢得最多的人最少赢的场数. 分析 二分最多人赢的场数 ...

  8. 【ZOJ】3329 One Person Game

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3754 题意:有三个色子,分别有k1.k2.k3个面,权值分别是1-k1, 1~ ...

  9. Ninject使用demo

    public class HomeController : Controller { public ActionResult Index() { //核心对象 IKernel ninjectKerne ...

  10. 使 Excel 规则更容易理解(Oracle Policy Modeling-Make your Excel rules easier to understand)

    有以下几种方法可以使 Excel 规则更容易理解. 在 Excel 工作簿中缩短属性名称 您可以使用图例关键字创建在 Excel 中引用属性的缩写方式.可以根据情况指定此缩写形式. 要指定图例关键字, ...