.NET Documentation

IEnumerable<T> Interface

public interface IEnumerable<out T> : System.Collections.IEnumerable
Namespace: System.Collections.Generic
Assembly: System.Runtime.dll

Represents a strongly typed collection that can be enumerated. The IEnumerable<T> interface is the base interface for all generic collections that can be enumerated.

Remarks

The IEnumerable<T> interface represents a collection that can be enumerated. It is the base interface for all generic collections that can be enumerated. The IEnumerable<T> interface is used to iterate over a collection of a specific type. The GetEnumerator method returns an IEnumerator<T> object that can be used to iterate through the collection.

The out keyword in the generic type parameter <out T> indicates that T is an output type, meaning that the generic type parameter can be contravariant. This allows you to use a more general type than originally specified (e.g., you can use an IEnumerable<string> where an IEnumerable<object> is expected).

Many LINQ extension methods operate on collections that implement IEnumerable<T>.

Members

Methods

  • System.Collections.Generic.IEnumerator<T> GetEnumerator()

    Returns an enumerator that iterates through the collection.

    System.Collections.IEnumerable GetEnumerator()

    Returns an enumerator that iterates through a collection. This is an explicit interface implementation of System.Collections.IEnumerable.GetEnumerator.

Inherited Members

This interface inherits the members of the System.Collections.IEnumerable interface.

  • System.Collections.IEnumerator GetEnumerator()

    Returns an enumerator that iterates through a collection.

Examples

The following C# code example demonstrates how to use the IEnumerable<T> interface to iterate over a list of strings.


using System;
using System.Collections.Generic;

public class Example
{
    public static void Main(string[] args)
    {
        IEnumerable<string> names = new List<string> { "Alice", "Bob", "Charlie" };

        foreach (string name in names)
        {
            Console.WriteLine(name);
        }
    }
}
                

See Also