Why does List implement IReadOnlyList in .NE

2019-01-08 00:16发布

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

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

7条回答
ゆ 、 Hurt°
2楼-- · 2019-01-08 01:17

The interfaces IReadOnlyList and IReadOnlyCollection are somewhat confusing because they do not mean that the collection is read-only, just that read-only access is supported. From the MSDN documentation (scroll down to Remarks)

The content of list elements is not guaranteed to be read-only.

A better name would be IReadable, see Why doesn't generic ICollection implement IReadOnlyCollection in .NET 4.5?. Furthermore, this implies that IList should inherit IReadOnlyList albeit it does not due to backwards compatibility, see Why doesn't IList<T> inherit from IReadOnlyList<T>?.

查看更多
登录 后发表回答