直接使用yield return关键字通过类似返回值的方式灵活地构造迭代器

 public class EmployeeCollection :IEnumerable<Employee>
{
private Employee[] employees;
#region IEnumerable<Employee>成员
public IEnumerator<Employee> GetEnumerator()
{
foreach(Employee employee in employees)
{
yield return employee;
}
}
#endregion
#region IEnumerable成员
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion }
05-22 16:47