CS1579 de erro do compilador

instrução foreach não pode operar em variáveis do tipo 'Type 1' porque 'Tipo2' não contém uma definição de pública para 'identificador'

Para iterar por meio de uma coleção usando o foreach instrução, a coleção deve atender aos seguintes requisitos:

Exemplo

Nesse exemplo, foreach não é capaz de fazer uma iteração através da coleção porque não há nenhum publicGetEnumerator método na MyCollection.

O exemplo a seguir gera CS1579.

// CS1579.cs
using System;
public class MyCollection 
{
   int[] items;
   public MyCollection() 
   {
      items = new int[5] {12, 44, 33, 2, 50};
   }

   // Delete the following line to resolve.
   MyEnumerator GetEnumerator()

   // Uncomment the following line to resolve:
   // public MyEnumerator GetEnumerator() 
   {
      return new MyEnumerator(this);
   }

   // Declare the enumerator class:
   public class MyEnumerator 
   {
      int nIndex;
      MyCollection collection;
      public MyEnumerator(MyCollection coll) 
      {
         collection = coll;
         nIndex = -1;
      }

      public bool MoveNext() 
      {
         nIndex++;
         return(nIndex < collection.items.GetLength(0));
      }

      public int Current 
      {
         get 
         {
            return(collection.items[nIndex]);
         }
      }
   }

   public static void Main() 
   {
      MyCollection col = new MyCollection();
      Console.WriteLine("Values in the collection are:");
      foreach (int i in col)   // CS1579
      {
         Console.WriteLine(i);
      }
   }
}