2010-01-26

IEnumerable, IEnumerator, GetEnumerator()

如果想要自定义类型支持foreach,那么此类型必须实现IEnumerable 接口,然后在GetEnumerator()方法中返回实现IEnumerator接口的对象,实现Current,MoveNext,Reset方法。好消息是2.0中加入了yield关键字,一句yield return,编译器就会给你做剩下的事情

using System;
using System.Collections;

namespace ConsoleApplication2
{
public class Person
{
public Person(string fName, string lName)
{
FirstName = fName;
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 = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
//return new PeopleEnum(_people);
// and you can delete below PeopleEnum class
for (int i = 0; i < _people.Length; i++)
{
yield return _people[i];
}
}
}

public class PeopleEnum : IEnumerator
{
public Person[] _people;

// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;

public PeopleEnum(Person[] list)
{
_people = list;
}

public bool MoveNext()
{
position++;
return (position < _people.Length);
}

public void Reset()
{
position = -1;
}

public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}

class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
new Person("Tom", "Cat"),
new Person("Jon", "Walker"),
new Person("Jet", "Li"),
};

People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.FirstName + " " + p.LastName);

}
}

或者是返回IEnumerable的方法

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
foreach (var p in Primes(100))
{
Console.WriteLine(p);
}
}

public static IEnumerable<int> Primes(int max)
{
yield return 2;
var found = new List<int> {3};
var candidate = 3;
while (candidate <= max)
{
var isPrime = found.TakeWhile(
prime => prime*prime <= candidate).All(prime => candidate%prime != 0);
if (isPrime)
{
found.Add(candidate);
yield return candidate;
}
candidate += 2;
}
}
}
}

Implementing iterators with yield statements

Behind the scenes of the C# yield keyword

No comments: