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

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

问题描述

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


  1. IReadOnlyCollection< T>

  2. IReadOnlyList< T>

  3. IReadOnlyDictionary< K,V>

我有点困惑为什么 HashSet< T> 不实施 IReadOnlyCollection< T> ?有什么原因,或者微软只是忘了设置了?



UPD



在两个小时后,我发现在BCL中有很多集合,它们具有 .Count 属性,但不实现 IReadOnlyCollection< T& / code>接口。



UPD2



此帖子 http://social.msdn.microsoft.com/论坛/ en / netfxbcl / thread / b4fb991a-3f5c-4923-93d4-7cd5c004f859 以及 Immo Landwerth的回答,他在其中说出以下内容


除了List<>和Dictionary<>之外的其他集合是否更新为
支持这些接口?



当然。事实上,我们所有的内置集合类型已经
实现了IReadOnlyList<>和IReadOnlyDictionary<> ;.这意味着,
可以直接将List,T []或Dictionary<>的一个实例传递给一个需要一个IReadOnly版本的
API。



解决方案

在4.5版的框架中, HashSet< T> 未实施 IReadOnlyCollection< out T& c $ c>。



这个遗漏在框架的4.6版本中解决了(在上面的问题提出后大约12个月后)。



这些更正是不限于 HashSet< T> ,其他集合如 Stack< T> 队列< T> 已获得这些改进。



<对于任何遗漏的原因的猜测是未决的。它可能是监督或时间压力,但坦率地说,它没有什么影响。我怀疑,即使我们喜欢相关的轶事,即使从微软开发团队的直接投入也会有些主观。


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天全站免登陆