Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 IEnumerator와 IEnumerable 인터페이스의 차이점

<시간/>

IEnumerable과 IEnumerator는 모두 C#의 인터페이스입니다.

IEnumerable은 IEnumerator 인터페이스를 반환하는 단일 메서드 GetEnumerator()를 정의하는 인터페이스입니다.

이것은 IEnumerable을 foreach 문과 함께 사용할 수 있다는 것을 구현하는 컬렉션에 대한 읽기 전용 액세스에 대해 작동합니다.

IEnumerator에는 MoveNext와 Reset의 두 가지 메서드가 있습니다. 또한 Current라는 속성도 있습니다.

다음은 IEnumerable 및 IEnumerator의 구현을 보여줍니다.

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의 두 가지 방법을 볼 수 있습니다.

// IEnumertor method
public bool MoveNext() {
   throw new NotImplementedException();
}

// IEnumertor method
public void Reset() {
   throw new NotImplementedException();
}