Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Non-generic IEnumerable and IEnumerator in System.Collections

IEnumerable provides an enumerator, and IEnumerator tracks the position in a sequence. An explicit implementation satisfies the non-generic contract without duplicating iteration logic.

Version: C# 12 and .NET 8; later runtimes may add compatible features

Syntax

using System.Collections;
using System.Collections.Generic;

sealed class Sequence<T> : IEnumerable<T>
{
    public IEnumerator<T> GetEnumerator()
    {
        yield break;
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

System.Collections.IEnumerable represents a source of elements; its GetEnumerator() returns a System.Collections.IEnumerator that tracks the position in the sequence. The enumerator starts before the first element. Read Current only after MoveNext() returns true, and stop when it returns false. Reset() is meant to restore the starting position, but it can throw NotSupportedException, as it does for yield iterators. To start over, get a new enumerator.

IEnumerable<T> in System.Collections.Generic inherits from non-generic IEnumerable, so the class must satisfy both contracts. Expose a public method returning IEnumerator<T> and add IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();. This explicit implementation is accessible through the non-generic interface. It is not an ordinary overload: C# does not allow overloading by return type alone. Both calls use the same iteration logic.

IEnumerator<T> inherits from IEnumerator and IDisposable. Its generic Current is typed as T; non-generic Current returns object. In the example, reading non-generic Current boxes an int, and foreach (int ...) unboxes it. A yield iterator supplies both versions of Current. If you write your own enumerator, you can implement non-generic Current explicitly.

foreach calls MoveNext() and reads Current without using Reset(). It also disposes the enumerator if it implements IDisposable, even when you exit the loop early. When iterating manually through IEnumerator, check for IDisposable in a finally block and call Dispose() if supported: the non-generic interface itself does not require it.

Examples

using System;
using System.Collections;
using System.Collections.Generic;

IEnumerable<int> typed = new EvenPair();
IEnumerable untyped = typed;

foreach (int value in typed)
    Console.WriteLine(FormattableString.Invariant($"Generic: {value}"));

foreach (int value in untyped)
    Console.WriteLine(FormattableString.Invariant($"Non-generic: {value}"));

sealed class EvenPair : IEnumerable<int>
{
    public IEnumerator<int> GetEnumerator()
    {
        yield return 2;
        yield return 4;
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

Result

Generic: 2
Generic: 4
Non-generic: 2
Non-generic: 4

Both variables refer to the same object. The second loop calls the explicit implementation through `IEnumerable` and reads `2` and `4` again.

Related lessons

  • C# · Lesson 38 · Intermediate · Version: 2

Sources