Maha

Maha

  • NA
  • 0
  • 326.1k

NP88 yield keyword

Mar 18 2008 9:26 AM

Hi Guys

 

NP88 yield keyword

 

http://www.java2s.com/Tutorial/CSharp/0220__Data-Structure/Asimpleexampleofaniterator.htm

 

I got this program from the above website. What is the function of yield keyword? Anyone knows please explain.

 

Thank you

 

using System;

using System.Collections;

 

class MyClass

{

   char[] chrs = { 'A', 'B', 'C', 'D' };

 

   public IEnumerator GetEnumerator()

   {

      foreach (char ch in chrs)

         yield return ch;

   }

}

 

class MainClass

{

   public static void Main()

   {

      MyClass mc = new MyClass();

 

      foreach (char ch in mc)

         Console.Write(ch + " ");

 

      Console.WriteLine();

   }

}

/*

A B C D

*/


Answers (2)