C# Interview Question #130
What is the difference between IEnumerable<T>, IReadOnlyCollection<T>, and IReadOnlyList<T>?
Advanced LINQ, Expressions & Collections Senior Advanced
Quick Interview Answer
IEnumerable<T> guarantees only that a sequence can be enumerated.
Detailed Explanation
IEnumerable<T> guarantees only that a sequence can be enumerated.
IReadOnlyCollection<T> adds a Count property, which is useful when consumers need the number of elements but should not modify the collection through the interface.
IReadOnlyList<T> further adds indexed access.
Returning a read-only interface can communicate that callers are not expected to modify a collection through that API. However, a read-only interface does not necessarily mean the underlying collection itself is immutable.
Code Example
public IReadOnlyList<Product> GetProducts()
{
return _products;
}