컴파일러 오류 CS1640
업데이트: 2007년 11월
오류 메시지
foreach 문은 'interface'의 여러 인스턴스를 구현하므로 'type' 형식의 변수에는 foreach 문을 수행할 수 없습니다. 특정 인터페이스 인스턴스에 캐스팅하십시오.
foreach statement cannot operate on variables of type 'type' because it implements multiple instantiations of 'interface', try casting to a specific interface instantiation
형식이 IEnumerator<T>의 여러 인스턴스에서 상속되므로 foreach에서 사용할 수 있는 형식의 고유한 열거형이 없습니다. IEnumerator<T>의 형식을 지정하거나 다른 루프 구문을 사용하십시오.
예제
다음 샘플에서는 CS1640 오류가 발생하는 경우를 보여 줍니다.
// CS1640.cs
using System;
using System.Collections;
using System.Collections.Generic;
public class C : IEnumerable, IEnumerable<int>, IEnumerable<string>
{
IEnumerator<int> IEnumerable<int>.GetEnumerator()
{
yield break;
}
IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
yield break;
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)((IEnumerable<string>)this).GetEnumerator();
}
}
public class Test
{
public static int Main()
{
foreach (int i in new C()){} // CS1640
// Try specifing the type of IEnumerable<T>
// foreach (int i in (IEnumerable<int>)new C()){}
return 1;
}
}