Implement IEnumerable with 'yield' : IEnumerable : Data Structure C# Examples


C# Examples » Data Structure » IEnumerable »

 

Implement IEnumerable with 'yield'









    
using  System;
using  System.Collections.Generic;
using  System.Text;

class  MyClass  :  IEnumerable<string>
{
      IEnumerator<string>  Letter
      {
            get
            {
                  yield  return  "A";
                  yield  return  "B";
                  yield  return  "C";
            }
      }

      public  IEnumerator<string>  GetEnumerator()
      {
            return  Letter;
      }

      System.Collections.IEnumerator  System.Collections.IEnumerable.GetEnumerator()
      {
            return  Letter;
      }
}

class  MainClass
{
      static  void  Main()
      {
            MyClass  mc1  =  new  MyClass();
            foreach  (string  s  in  mc1)
                  Console.Write("{0}  ",  s);
      }
}
    
   
  
   



Output

A B C


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» IEnumerable