16

I've just found that .NET Fx now has 3 useful interfaces:

  1. IReadOnlyCollection<T>
  2. IReadOnlyList<T>
  3. IReadOnlyDictionary<K,V>

And I'm bit confused why HashSet<T> do not implement IReadOnlyCollection<T>? Are there any reasons, or Microsoft just forgot about sets again?

UPD

After two-hours googling I've found that there are many collections in BCL which has .Count property but do not implement IReadOnlyCollection<T> interface.

UPD2

I've found this post http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/b4fb991a-3f5c-4923-93d4-7cd5c004f859 and the answer by Immo Landwerth where he've said following

Will other collections besides List<> and Dictionary<> be updated to support these interfaces?

Absolutely. In fact, all of our built-in collection types already implement IReadOnlyList<> and IReadOnlyDictionary<>. This means, you can directly pass an instance of List, T[] or Dictionary<> to an API that takes an IReadOnly-version of it.

4

1 回答 1

16

在 4.5 版本的框架中,HashSet<T>没有实现IReadOnlyCollection<out T>.

这个遗漏在框架的 4.6 版本中得到了解决(在提出上述问题近 12 个月后发布)。

这些更正不限于HashSet<T>其他集合,例如Stack<T>Queue<T>已收到这些改进。

关于任何遗漏的原因的猜测是没有实际意义的。这可能是疏忽或时间压力,但坦率地说,这没什么大不了的。我怀疑即使是来自 Microsoft 开发团队的直接输入也会有些主观,即使我们喜欢相关的轶事。

于 2015-07-30T15:53:39.260 回答