Implement IEnumerable with loop : IEnumerable « Data Structure « C# / CSharp Tutorial






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);
   }
}
A B C








11.43.IEnumerable
11.43.1.Implement IEnumerable with 'yield'
11.43.2.Implement IEnumerable with loop
11.43.3.Provide different IEnumerable implemetation for one class
11.43.4.yield value for IEnumerable
11.43.5.Get list of integer whose value is higher than 10
11.43.6.Iterator Block Iteration Sample
11.43.7.Counting Enumerable