A simple example of an iterator. : IEnumerator : Data Structure C# Examples


C# Examples » Data Structure » IEnumerator »

 

A simple example of an iterator.









    
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();  
    }  
}
    
   
  
   



Output

A B C D


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Structure
» IEnumerator