#559 – An Iterator Can Return an Enumerable Type or an Enumerator

An iterator is a block of code that contains one or more yield statements.  It can return one of the following interfaces:

  • IEnumerable  (in System.Collections)
  • IEnumerable<T>  (in System.Collections.Generic)
  • IEnumerator  (in System.Collections)
  • IEnumerator<T>  (in System.Collections.Generic)

You’d return an enumerable type when you want provide client code with a collection of objects and you don’t want to write your own custom collection class.

You’d typically return an enumerator when you’re implementing the GetEnumerator method in a custom collection class.

 

Advertisement