Junior Collections

What is the use of the IEnumerable<T> interface in C# collections?

IEnumerable<T> is the base interface for all generic collections that can be enumerated

(looped over). It allows the use of foreach loops and LINQ queries.

It defines a single method:

IEnumerator<T> GetEnumerator();

Example:

List<string> items = new List<string> { "A", "B", "C" };

foreach (string item in items) // IEnumerable<string> in action

Console.WriteLine(item);

Real-world use case:

When reading product data from a list or querying a database, IEnumerable<T> allows

deferred execution and efficient data processing using LINQ.

Follow:

More from C# Programming Tutorial

All questions for this course