System.Collections.IEnumerable Interface

Namespace: System.Collections

The IEnumerable interface is the base interface for all collection types in the .NET Framework. It provides a way to iterate over a collection, one element at a time.

Summary

The IEnumerable interface represents a generic collection that can be enumerated. It is implemented by classes such as List<T>, Dictionary<TKey, TValue>, and arrays.

Syntax

public interface IEnumerable
{
IEnumerator GetEnumerator();
}

Members

Methods

Remarks

The IEnumerable interface is the foundation for iterating over collections in C#. When you use a foreach loop in C#, the compiler generates code that calls the GetEnumerator() method of the collection to obtain an enumerator, and then uses the IEnumerator interface to step through the collection's elements.

If a collection implements IEnumerable, it means that you can iterate over its elements using a foreach loop.

Requirements

Assembly

mscorlib.dll

Namespace

System.Collections

See Also

Example

The following C# code example demonstrates how to use a collection that implements IEnumerable with a foreach loop.

using System; using System.Collections; using System.Collections.Generic; public class Example { public static void Main() { // A List implements IEnumerable List<string> fruits = new List<string>() { "Apple", "Banana", "Cherry" }; Console.WriteLine("Fruits:"); // The foreach loop utilizes the IEnumerable interface foreach (string fruit in fruits) { Console.WriteLine(fruit); } } }