标签:
http://blog.csdn.net/byondocean/article/details/6871881
IEnumerable和IEnumerator两个都是接口
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
bool MoveNext(); //将游标的内部位置向前移动
object Current{get;} //获取当前的项(只读属性)
void Reset(); //将游标重置到第一个成员前面
}
Enumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。IEnumerator接口有什么呢?它是一个真正的集合访问器,没有它,就不能使用foreach语句遍历集合或数组
IEnumerator接口定义了一个Current属性,MoveNext和Reset两个方法
public class Garage
{
Car[] carArray = new Car[4]; //在Garage中定义一个Car类型的数组carArray,其实carArray在这里的本质是一个数组字段
//启动时填充一些Car对象
public Garage()
{
//为数组字段赋值
carArray[0] = new Car("Rusty", 30);
carArray[1] = new Car("Clunker", 50);
carArray[2] = new Car("Zippy", 30);
carArray[3] = new Car("Fred", 45);
}
}
//这看起来好像是可行的
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*********Fun with IEnumberable/IEnumerator************\n");
Garage carLot = new Garage();
//交出集合中的每一Car对象吗
foreach (Car c in carLot)
{
Console.WriteLine("{0} is going {1} MPH", c.CarName, c.CurrentSpeed);
}
Console.ReadLine();
}
}
让人沮丧的是,编译器通知我们Garage类没有实现名为GetEnumerator()的方法
要想Garage类也可以使用foreach遍历其中的项,那我们就要修改Garage类型使之支持这些接口,可以手工实现每一个方法,不过这得花费不少功夫。虽然自己开发GetEnumerator()、MoveNext()、Current和Reset()也没有问题,但有一个更简单的办法。因为System.Array类型和其他许多类型(如List)已经实现了IEnumerable和IEnumerator接口,你可以简单委托请求到System.Array,
namespace MyCarIEnumerator
{
public class Garage:IEnumerable
{
Car[] carArray = new Car[4];
//启动时填充一些Car对象
public Garage()
{
carArray[0] = new Car("Rusty", 30);
carArray[1] = new Car("Clunker", 50);
carArray[2] = new Car("Zippy", 30);
carArray[3] = new Car("Fred", 45);
}
public IEnumerator GetEnumerator()
{
return this.carArray.GetEnumerator();
}
}
}
//修改Garage类型之后,就可以在C#foreach结构中安全使用该类型了。
//除此之外,GetEnumerator()被定义为公开的,对象用户可以与IEnumerator类型交互:
namespace MyCarIEnumerator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*********Fun with IEnumberable/IEnumerator************\n");
Garage carLot = new Garage();
//交出集合中的每一Car对象吗
foreach (Car c in carLot) //之所以遍历carLot,是因为carLot.GetEnumerator()返回的项时Car类型,这个十分重要
{
Console.WriteLine("{0} is going {1} MPH", c.CarName, c.CurrentSpeed);
}
Console.WriteLine("GetEnumerator被定义为公开的,对象用户可以与IEnumerator类型交互,下面的结果与上面是一致的");
//手动与IEnumerator协作
IEnumerator i = carLot.GetEnumerator();
while (i.MoveNext())
{
Car myCar = (Car)i.Current;
Console.WriteLine("{0} is going {1} MPH", myCar.CarName, myCar.CurrentSpeed);
}
Console.ReadLine();
}
}
}
下面我们来看看手工实现IEnumberable接口和IEnumerator接口中的方法:
namespace ForeachTestCase
{
//继承IEnumerable接口,其实也可以不继承这个接口,只要类里面含有返回IEnumberator引用的GetEnumerator()方法即可
class ForeachTest:IEnumerable {
private string[] elements; //装载字符串的数组
private int ctr = 0; //数组的下标计数器
/// <summary>
/// 初始化的字符串
/// </summary>
/// <param name="initialStrings"></param>
ForeachTest(params string[] initialStrings)
{
//为字符串分配内存空间
elements = new String[8];
//复制传递给构造方法的字符串
foreach (string s in initialStrings)
{
elements[ctr++] = s;
}
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="source">初始化的字符串</param>
/// <param name="delimiters">分隔符,可以是一个或多个字符分隔</param>
ForeachTest(string initialStrings, char[] delimiters)
{
elements = initialStrings.Split(delimiters);
}
//实现接口中得方法
public IEnumerator GetEnumerator()
{
return new ForeachTestEnumerator(this);
}
private class ForeachTestEnumerator : IEnumerator
{
private int position = -1;
private ForeachTest t;
public ForeachTestEnumerator(ForeachTest t)
{
this.t = t;
}
#region 实现接口
public object Current
{
get
{
return t.elements[position];
}
}
public bool MoveNext()
{
if (position < t.elements.Length - 1)
{
position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
position = -1;
}
#endregion
}
static void Main(string[] args)
{
// ForeachTest f = new ForeachTest("This is a sample sentence.", new char[] { ‘ ‘, ‘-‘ });
ForeachTest f = new ForeachTest("This", "is", "a", "sample", "sentence.");
foreach (string item in f)
{
System.Console.WriteLine(item);
}
Console.ReadKey();
}
}
}
另一种接口实现方式:
http://www.cnblogs.com/shaosks/archive/2011/09/27/2193270.html
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string firstName, string lastName)
{
this.FirstName = firstName;
this.LastName = lastName;
}
}
另外通过People类来实现IEnumerable和IEnumerator接口.
//实现IEnumerable接口
public class People :IEnumerable
{
public Person [] pers;
public People(Person [] ps)
{
this.pers = ps;
}
public IEnumerator GetEnumerator()
{
//foreach(Person p in pers)
// {
// yield return p;
// }
return new People1(pers);
}
}
//实现IEnumerator接口
public class People1 : IEnumerator
{
public Person[] pers;
public People1(Person[] per)
{
this.pers = per;
}
int position = -1;
public bool MoveNext()
{
position++;
return position < pers.Length;
}
public void Reset()
{
position=-1;
}
public object Current
{
get
{
try
{
return pers[position];
}
catch(IndexOutOfRangeException ex)
{
throw new InvalidOperationException();
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/notlate/p/4425621.html