What does ICollection<T> provide, and how does it differ from IEnumerable<T>?
Short answer: ICollection<T> extends IEnumerable<T> and adds features like: Counting (Count property) Adding and removing items (Add, Remove) Checking for existence (Contains) Difference: IEnumerable<T> is read-only and forward-only iteration. ICollection<T> adds modification capabilities.
Example code
ICollection<int> numbers = new List<int>(); numbers.Add(5); numbers.Remove(5); Console.WriteLine(numbers.Count); Real-world use case: Use ICollection<T> when you need to manipulate the collection (add/remove items), such as managing an in-memory cart of products in a shopping application.
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