IEnumerable Interface

This documentation applies to the .NET Framework.

Namespace: System.Collections

Summary

Represents a strongly typed collection that can be enumerated. The IEnumerable interface is the base interface for all collections in the .NET Framework. It provides a way to iterate through a collection of items.

The generic version, IEnumerable<T>, provides a strongly typed enumeration experience and is generally preferred for type safety.

Syntax

public interface IEnumerable : ICollection, IEnumerable
{
    // Methods
    IEnumerator GetEnumerator();
}

Remarks

The IEnumerable interface has a single method, GetEnumerator, which returns an IEnumerator object. The IEnumerator interface allows you to iterate through the collection. The foreach loop statement in C# is syntactic sugar for using the IEnumerable and IEnumerator interfaces.

Collections that implement IEnumerable can be iterated over using a foreach loop. This includes arrays, lists, dictionaries, and many other collection types.

It's important to note that iterating through a collection using IEnumerable does not guarantee that the collection will not change during iteration. If the collection is modified while it is being enumerated, an InvalidOperationException may be thrown.

Important Considerations

The IEnumerable interface is fundamental to LINQ (Language Integrated Query). Many LINQ extension methods operate on collections that implement IEnumerable.

Methods

Member Description
GetEnumerator() Returns an enumerator that iterates through the collection.

Implementing IEnumerable

When you implement the IEnumerable interface, you need to provide an implementation for the GetEnumerator() method. This method should return an object that implements the IEnumerator interface.

Example: Implementing IEnumerable

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

public class MyCollection<T> : IEnumerable<T>
{
    private List<T> _items = new List<T>();

    public void Add(T item)
    {
        _items.Add(item);
    }

    // Explicit implementation of IEnumerable.GetEnumerator()
    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }

    // Implementation of IEnumerable<T>.GetEnumerator()
    public IEnumerator<T> GetEnumerator()
    {
        return _items.GetEnumerator();
    }
}

Examples of Usage

The foreach loop is the most common way to use collections that implement IEnumerable.

Example: Iterating with foreach

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main(string[] args)
    {
        MyCollection<string> myStrings = new MyCollection<string>();
        myStrings.Add("Hello");
        myStrings.Add("World");
        myStrings.Add("!");

        foreach (string str in myStrings)
        {
            Console.WriteLine(str);
        }
    }
}

Output:

Hello
World
!

Tip

For modern .NET development, consider using the generic IEnumerable<T> and the extensive features of LINQ.

Related Interfaces