能使用foreach语句进行遍历的必须是实现了IEnuerable接口的类型,IEnumerable只有一个方法:IEnumerator GetEnumerator();IEnumerator有三个方法 object Current()//获取当前对象,bool MoveNext()//将游标移到下一个位置,void Reset()//将光标重置的第一个成员前面。以下是我写的一个例子。using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;namespace ForeachAble
{
class Program
{
static void Main(string[] args)
{
CarStore _4s= new CarStore();
foreach (Car item in _4s)
{
Console.WriteLine(item.CarName);
}
Console.ReadKey();
}
} public class Car
{
private string _name;
public string CarName
{
get
{
return this._name;
}
set
{
_name = value;
}
} public Car(string name)
{
this._name = name;
}
}
public class CarStore:IEnumerable
{
private Car[] _cars;
public Car[] Cars
{
get
{
return _cars;
}
set
{
_cars = value;
}
} public CarStore()
{
_cars = new Car[] { new Car("吉利"),new Car("比亚迪"),new Car("力帆"),new Car("奔腾B70"),new Car("红旗")};
} public IEnumerator GetEnumerator()
{
return new CarEnumertor(this);
}
public class CarEnumertor : IEnumerator
{
int _index;
CarStore _newCarStore;
public CarEnumertor(CarStore newCarStore)
{
_index = -1;
_newCarStore = newCarStore;
}
public object Current
{
get { return _newCarStore._cars[_index]; }
} public bool MoveNext()
{
if (_index < _newCarStore._cars.Length-1)
{
_index++;
return true;
}
else
{
return false;
}
} public void Reset()
{
_index=-1;
}
}
}
}
运行通过,显示出了5个car对象,但有一个疑惑的是关于 Reset方法,无论我将_index的值改为-1,0,1,甚至什么都不做,都没有影响最后结果,好像Reset方法没有执行一样。反而是构造函数的_index赋值结果会影响执行结果,将_index=0,第一个car对象就不会显示。不知为什么?
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;namespace ForeachAble
{
class Program
{
static void Main(string[] args)
{
CarStore _4s= new CarStore();
foreach (Car item in _4s)
{
Console.WriteLine(item.CarName);
}
Console.ReadKey();
}
} public class Car
{
private string _name;
public string CarName
{
get
{
return this._name;
}
set
{
_name = value;
}
} public Car(string name)
{
this._name = name;
}
}
public class CarStore:IEnumerable
{
private Car[] _cars;
public Car[] Cars
{
get
{
return _cars;
}
set
{
_cars = value;
}
} public CarStore()
{
_cars = new Car[] { new Car("吉利"),new Car("比亚迪"),new Car("力帆"),new Car("奔腾B70"),new Car("红旗")};
} public IEnumerator GetEnumerator()
{
return new CarEnumertor(this);
}
public class CarEnumertor : IEnumerator
{
int _index;
CarStore _newCarStore;
public CarEnumertor(CarStore newCarStore)
{
_index = -1;
_newCarStore = newCarStore;
}
public object Current
{
get { return _newCarStore._cars[_index]; }
} public bool MoveNext()
{
if (_index < _newCarStore._cars.Length-1)
{
_index++;
return true;
}
else
{
return false;
}
} public void Reset()
{
_index=-1;
}
}
}
}
运行通过,显示出了5个car对象,但有一个疑惑的是关于 Reset方法,无论我将_index的值改为-1,0,1,甚至什么都不做,都没有影响最后结果,好像Reset方法没有执行一样。反而是构造函数的_index赋值结果会影响执行结果,将_index=0,第一个car对象就不会显示。不知为什么?
CarStore cars = new CarStore();
IEnumerator ienum = cars.GetEnumerator();Car car;
if (ienum.MoveNext()) car = ienum.Current as Car; // car = 吉利
if (ienum.MoveNext()) car = ienum.Current as Car; // car = 比亚迪ienum.Reset(); // <--------
if (ienum.MoveNext()) car = ienum.Current as Car; // car = 吉利
{
CarStore _4s = new CarStore();
IEnumerator e = _4s.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine((e.Current as Car).CarName);
}
Console.WriteLine(e.MoveNext());
e.Reset();
Console.WriteLine(e.MoveNext());
Console.ReadKey();
}