A generic iterator. : Generic IEnumerator : Generic C# Examples


C# Examples » Generic » Generic IEnumerator »

 

A generic iterator.









    
using  System;  
using  System.Collections.Generic;  
  
class  MyClass<T>  {  
    T[]  array;  
  
    public  MyClass(T[]  a)  {  
        array  =  a;  
    }  
  
    public  IEnumerator<T>  GetEnumerator()  {  
        foreach(T  obj  in  array)  
            yield  return  obj;  
    }  
}  
  
class  MainClass  {  
    public  static  void  Main()  {  
        int[]  nums  =  {  4,  3,  6,  4,  7,  9  };  
        MyClass<int>  mc  =  new  MyClass<int>(nums);  
  
        foreach(int  x  in  mc)  
            Console.Write(x  +  "  ");  
  
        Console.WriteLine();  
  
  
      bool[]  bVals  =  {  true,  true,  false,  true  };  
        MyClass<bool>  mc2  =  new  MyClass<bool>(bVals);  
  
        foreach(bool  b  in  mc2)  
            Console.Write(b  +  "  ");  
  
        Console.WriteLine();  
    }  
}
    
   
  
   



Output

4 3 6 4 7 9
True True False True


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Generic
» Generic IEnumerator