What is the use of the IEnumerable<T> interface in C# collections?
Short answer: 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" };
Example code
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.
Real-world example (ShopNest)
In ShopNest, an order page loads products with List<Product> so you can Add items to the cart and access them by index. Use IEnumerable<T> when you only need to loop (for example, printing invoice lines).
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png