Implement IEnumerable with loop : IEnumerable : Data Structure C# Examples


C# Examples » Data Structure » IEnumerable »

 

Implement IEnumerable with loop









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

class  MyClass  :  IEnumerable<string>
{
      IEnumerator<string>  Letter
      {
            get
            {
                  string[]  s  =  {  "A",  "B",  "C"  };
                  for  (int  i  =  0;  i  <  s.Length;  i++)
                        yield  return  s[i];
            }
      }

      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