Share via


컴파일러 오류 CS1579

업데이트: 2007년 11월

오류 메시지

'type2'에 'identifier'에 대한 공개 정의가 없으므로 'type1' 형식의 변수에는 foreach 문을 수행할 수 없습니다.
foreach statement cannot operate on variables of type 'type1' because 'type2' does not contain a public definition for 'identifier'

foreach 문을 사용하여 컬렉션을 반복 실행하려면 컬렉션이 다음 요구 사항을 충족해야 합니다.

예제

다음 샘플에서는 MyCollection에 publicGetEnumerator 메서드가 없으므로 foreach는 컬렉션을 반복 실행할 수 없습니다.

다음 샘플에서는 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);
      }
   }
}