为什么名单< T>实施IReadOnlyList< T>在.NET 4.5?名单、LT、GT、NET

2023-09-02 02:01:21 作者:槑。

为什么名单,其中,T> 实施 IReadOnlyList< T> 在.NET 4.5

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

名单,其中,T&GT; 不是只读...

推荐答案

由于名单,其中,T&GT; 实现了所有必要的方法/属性/等。 (然后一些) IReadOnlyList&LT的; T&GT; 。接口是一个合同,说:我可以做的至少的这些事情。

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."

的文档 IReadOnlyList&LT; T&GT; 表示,重新presents元素的只读集合。

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

这是正确的。有在该界面没有mutator方法。这就是只读手段,对不对? IReadOnlyList&LT; T&GT; 采用的是典型(合同)的方式,而不是作为一个的标记。

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.