2
Answers

NP88 yield keyword

Maha

Maha

16y
1.9k
1

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)