最終更新:2009-09-10 (木) 15:22:34 (5348d)  

yeild
Top / yeild

  • yeild returnの所で一度呼び出し元に処理が帰る
  • 処理をうち切りたい場合はyeild breakする。
class Sample
{
  public IEnumerator<char> GetEnumerator()
  {
    Console.Write("[Cを返す]");
    yield return 'C';

    Console.Write("[#を返す]");
    yield return '#';
  }
}
class Program
{
  static void Main(string[] args)
  {
    foreach (char c in new Sample())
    {
      Console.Write("{0}", c);
    }
    // 出力:[Cを返す]C[#を返す]#
  }
}