Junior
From PDF
Collections
C# Collections
What is the purpose of IReadOnlyCollection<T> and IReadOnlyList<T> interfaces?
Short answer: These interfaces provide read-only access to collections: IReadOnlyCollection<T> provides Count and enumeration. IReadOnlyList<T> provides index-based access without modification.
Example code
IReadOnlyList<int> ids = new List<int> { 1, 2, 3 }; Console.WriteLine(ids[0]); // Access element // ids[0] = 10; // Compile-time error – read-only Real-world use case: Useful in APIs where you want to expose collection data but prevent consumers from modifying it—e.g., returning a list of supported currencies from a service.
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