C# Interview Question #37
What is IEnumerable<T> in C#?
Generics & Collections Mid-Level Intermediate
Quick Interview Answer
IEnumerable<T> represents a sequence that can be enumerated one element at a time. It is the fundamental generic interface used by foreach and many LINQ operations.
Detailed Explanation
IEnumerable<T> represents a sequence that can be enumerated one element at a time. It is the fundamental generic interface used by foreach and many LINQ operations.
It exposes GetEnumerator() and is suitable when callers primarily need to iterate over a sequence rather than modify it.
Many LINQ operators return IEnumerable<T> and use deferred execution, meaning the query may not execute until the sequence is enumerated.
Code Example
IEnumerable<int> numbers =
new List<int> { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}