为什么HashSet的< T>没有实现IReadOnlyCollection< T>?LT、HashSet、IReadOnlyCollection、GT

2023-09-04 00:37:31 作者:我很骄傲

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

IReadOnlyCollection<T> IReadOnlyList&LT; T&GT; IReadOnlyDictionary<K,V>

和我有点困惑,为什么HashSet<T>不执行 IReadOnlyCollection&LT; T&GT; ?是否有任何理由,或微软刚刚忘了套了吗?

UPD

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

UPD2

我发现这个职位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版本吧。

解决方案 C ,没有语法错误,但是实现不了指定功能,尤其是functwo函数好像有问题,请大神指教,谢谢

在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:

IReadOnlyCollection<T> IReadOnlyList<T> 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.