Why does List<T> implement IReadOnlyList<T> interface?

.net, c#

Solution

It allows you to expose a read only "proxy" of that list, so that you can pass that interface reference elsewhere and know that the code won't mutate the list. (Technically it can try to cast it back to something like `List` and mutate it, but it shouldn't do that.)

It also allows a method to specifically indicate that while it needs to accept a list, it won't mutate it.

Having a read-only interface also allows that interface to be covariant, unlike `List` or `IList`.

Problem

Why `List<T>` implements `IReadOnlyList<T>` even though `List<T>` is not read only?

Original source

Related problems