C# IEnumerable Interface

Beginner
5 min read
Updated: Nov 2026
3 examples
Interface

Definition

In C#, IEnumerable<T> means "I am a sequence you can loop over." Arrays, lists, strings, queues, and stacks all implement it.

A foreach loop secretly calls GetEnumerator(), then calls MoveNext() and reads Current again and again until the sequence ends.

Syntax

The interface has one method.

C#
public interface IEnumerable<out T>
{
    IEnumerator<T> GetEnumerator();
}
CallWhat it does
foreach (var x in items)Loops over any IEnumerable.
items.GetEnumerator()Gets the enumerator to walk the items by hand.
IEnumerable<int> numbersA parameter type that accepts arrays, lists, and more.

Collections have their own guides, for example String.GetEnumerator().

Members

MemberDescription
IEnumerable<T>.GetEnumerator()Returns an enumerator that can walk through the items.
IEnumerator<T>.CurrentThe item the enumerator is pointing at right now.
IEnumerator<T>.MoveNext()Moves to the next item. Returns false when there are no more.
IEnumerator<T>.Reset()Moves back to the start. Rarely used.

C# Examples

Example 1 — foreach over Different Types

The same loop works for a list and a string.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var fruits = new List<string> { "apple", "pear" };

        foreach (string f in fruits) {
            Console.WriteLine(f);
        }

        foreach (char c in "Hi") {
            Console.WriteLine(c);
        }
    }
}

How It Works

  1. List is IEnumerable. foreach visits each item in order.
  2. String is IEnumerable<char>. The second loop visits each character.
  3. One loop style. You do not need to know how each collection stores data.

Example 2 — Walk Items by Hand

Do what foreach does yourself with GetEnumerator().

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var numbers = new List<int> { 10, 20, 30 };

        IEnumerator<int> e = numbers.GetEnumerator();

        while (e.MoveNext()) {
            Console.WriteLine(e.Current);
        }
    }
}

How It Works

  1. Get the enumerator. It starts before the first number.
  2. MoveNext() steps forward. It returns true while an item exists.
  3. Current reads the item. The loop ends when MoveNext() returns false.

Example 3 — Create Your Own Sequence

A method that returns IEnumerable<int> can hand out values one at a time with yield return.

C#
using System;
using System.Collections.Generic;

class Program {
    static IEnumerable<int> CountTo(int n) {
        for (int i = 1; i <= n; i++) {
            yield return i;
        }
    }

    static void Main() {
        foreach (int number in CountTo(3)) {
            Console.WriteLine(number);
        }
    }
}

How It Works

  1. Return type. IEnumerable<int> says the method produces a sequence of numbers.
  2. yield return. Hands back one value, then pauses until the loop asks for the next.
  3. No list needed. The numbers are produced on demand.

Frequently Asked Questions

It is an interface for any sequence that can be looped over, such as arrays, lists, and strings.
foreach calls GetEnumerator(), then repeatedly calls MoveNext() and reads Current.
IEnumerable is the sequence. IEnumerator is the cursor that walks through it.
It lets a method return items one by one, pausing between each, without building a full list first.
No. Adding or removing items during foreach throws an InvalidOperationException.

Next: TypeCode

See the enum that names the basic .NET types.

TypeCode tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful