인터페이스는 인터페이스의 구성원인 속성, 메서드 및 이벤트를 정의합니다. 인터페이스에는 구성원의 선언만 포함됩니다.
C#의 일부 인터페이스 유형은 다음과 같습니다.
-
IEnumerable − 모든 일반 컬렉션에 대한 기본 인터페이스.
-
I목록 − 배열 및 목록 유형으로 구현되는 일반 인터페이스.
-
사전 - 사전 모음집.
IEnumerable은 IEnumerator 인터페이스를 반환하는 단일 메서드 GetEnumerator를 정의하는 인터페이스입니다.
이는 IEnumerable을 foreach 문과 함께 사용할 수 있음을 구현하는 컬렉션에 대한 읽기 전용 액세스에 대해 작동합니다.
다음은 IEnumerable 인터페이스의 구현을 보여줍니다.
예시
class Demo : IEnumerable, IEnumerator {
// IEnumerable method GetEnumerator()
IEnumerator IEnumerable.GetEnumerator() {
throw new NotImplementedException();
}
public object Current {
get { throw new NotImplementedException(); }
}
// IEnumertor method
public bool MoveNext() {
throw new NotImplementedException();
}
// IEnumertor method
public void Reset() {
throw new NotImplementedException();
}
} 위에서 IEnumerator의 두 가지 방법을 볼 수 있습니다.
// IEnumerator method
public bool MoveNext() {
throw new NotImplementedException();
}
// IEnumertor method
public void Reset() {
throw new NotImplementedException();
}