LINQ Interview Question #11

What is the IEnumerable interface used for?

LINQ Fundamentals, Syntax, IEnumerable & IQueryable Junior Beginner

Quick Interview Answer

The non-generic IEnumerable interface represents an object that can provide an enumerator. Generic IEnumerable<T> adds strong typing and is preferred for modern C# collections.

Detailed Explanation

The non-generic IEnumerable interface represents an object that can provide an enumerator. Generic IEnumerable<T> adds strong typing and is preferred for modern C# collections.

foreach relies on enumeration behavior. LINQ's generic standard query operators are designed primarily around IEnumerable<T> and IQueryable<T>.

Code Example

IEnumerable<string> names =
    new List<string> { "Ali", "Sara", "John" };

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