为什么HashSet的< T>没有实现IReadOnlyCollection< T>? [英] Why HashSet<T> does not implement IReadOnlyCollection<T>?

查看:173
本文介绍了为什么HashSet的< T>没有实现IReadOnlyCollection< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现,.NET Fx的,现在有3个有用的界面:

  1. <一个href="http://msdn.microsoft.com/en-us/library/hh881542"><$c$c>IReadOnlyCollection<T>
  2. IReadOnlyList&LT; T&GT;
  3. <一个href="http://msdn.microsoft.com/en-us/library/hh136548"><$c$c>IReadOnlyDictionary<K,V>

和我有点困惑,为什么<一href="http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29"><$c$c>HashSet<T>不执行 IReadOnlyCollection&LT; T&GT; ?是否有任何理由,或微软刚刚忘了套了吗?

UPD

在两小时google搜索,我发现有很多藏品BCL拥有 .Count之间财产,但不执行 IReadOnlyCollection&LT; T&GT ;。接口

UPD2

我发现这个职位<一个href="http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/b4fb991a-3f5c-4923-93d4-7cd5c004f859">http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/b4fb991a-3f5c-4923-93d4-7cd5c004f859和答案的 IMMO Landwerth 的he've所说的下面,其中

  

请问除了名单,其中其他收藏;>词典&LT;>更新为   支持这些接口?

     

当然可以。事实上,我们的所有内置集合类型的已   实施IReadOnlyList&LT;>和IReadOnlyDictionary&LT;>。这意味着,就   可以直接通过列表中,T []或字典℃的实例;>到   API,它需要一个IReadOnly版本吧。

解决方案

在4.5版本的框架,的 的HashSet&LT; T&GT; 不执行 IReadOnlyCollection&LT;出T&GT;

这遗漏是在框架4.6版本解决了(上面的问题近12个月后释放的要求)。

这些更正不限于的HashSet&LT; T&GT; ,其他收藏品如的 堆栈&LT; T&GT; 和的 问答LT; T&GT; 收到这些改进

揣测任何遗漏的原因是没有实际意义。这可能是疏忽或时间pressure但坦率地说,这是无关紧要的。我怀疑从微软开发团队甚至直接输入就有些主观,即使我们享受相关的轶事。

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.

解决方案

In version 4.5 of the framework, HashSet<T> does not implement IReadOnlyCollection<out T>.

This omission was resolved in version 4.6 of the framework (released almost 12 months after the above question was asked).

These corrections are not limited to HashSet<T>, other collections such as Stack<T> and Queue<T> have received these improvements.

Speculation on the reason for any omission is moot. It may be oversight or time pressure but frankly, it is of little consequence. I suspect that even direct input from the Microsoft Development Team would be somewhat subjective, even if we enjoy associated anecdotes.

这篇关于为什么HashSet的&LT; T&GT;没有实现IReadOnlyCollection&LT; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆