Why does List implement IReadOnlyList in .NET 4.5?

后端 未结 7 1533
离开以前
离开以前 2020-11-29 11:56

Why does List implement IReadOnlyList in .NET 4.5?

List isn\'t read only...

7条回答
  •  时光说笑
    2020-11-29 12:27

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

    The documentation for IReadOnlyList 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 is used in the "typical" (contract) way, not as a marker.

提交回复
热议问题