Why does List<T> implement IReadOnlyList<T> in .NET 4.5?

James Newton-King picture James Newton-King · Mar 7, 2013 · Viewed 9.3k times · Source

Why does List<T> implement IReadOnlyList<T> in .NET 4.5?

List<T> isn't read only...

Answer

Matt Ball picture Matt Ball · Mar 7, 2013

Because List<T> implements all of the necessary methods/properties/etc. (and then some) of IReadOnlyList<T>. An interface is a contract that says "I can do at least these things."

The documentation for IReadOnlyList<T> says it represents a read-only collection of elements.

That's right. There are no mutator methods in that interface. That's what read-only means, right? IReadOnlyList<T> is used in the "typical" (contract) way, not as a marker.