Create Reverse Iterator : Generic List : Generic C# Examples


C# Examples » Generic » Generic List »

 

Create Reverse Iterator









    
using  System;
using  System.Collections.Generic;

public  class  MainClass
{
        static  void  Main()  {
                List<int>  intList  =  new  List<int>();
                intList.Add(  1  );
                intList.Add(  2  );
                intList.Add(  3  );
                intList.Add(  4  );

                foreach(  int  n  in  CreateReverseIterator(intList)  )  {
                        Console.WriteLine(  n  );
                }
        }

        static  IEnumerable<T>  CreateReverseIterator<T>(  IList<T>  list  )  {
                int  count  =  list.Count;
                for(  int  i  =  count-1;  i  >=  0;  --i  )  {
                        yield  return  list[i];
                }
        }
}
    
   
  
   



Output

4
3
2
1


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Generic
» Generic List