为什么要使用HashSet& T& gt;不能实现IReadOnlyCollection< T& gt ;? [英] Why HashSet<T> does not implement IReadOnlyCollection<T>?

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

问题描述

我刚刚发现.NET Fx现在具有3个有用的接口:


  1. IReadOnlyCollection< T>

  2. IReadOnlyList< T>

  3. IReadOnlyDictionary< K ,V>

我有点困惑为什么 HashSet< T> 不实现 IReadOnlyCollection< T> ?有什么原因,还是微软又忘记了设置?



UPD



经过两个小时的谷歌搜索,我发现BCL中有许多具有 .Count 属性但不实现 IReadOnlyCollection< T> 界面。



UPD2



我发现了这篇文章 http://social.msdn .microsoft.com / Forums / en / netfxbcl / thread / b4fb991a-3f5c-4923-93d4-7cd5c004f859 以及 Immo Landwerth 的回答,他说的是以下内容

b
$ b


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



绝对。实际上,我们所有的内置集合类型
都实现了IReadOnlyList<>和IReadOnlyDictionary<>。这意味着,您
可以直接将List,T []或Dictionary<>的实例传递给采用IReadOnly版本的
API。



解决方案

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



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



这些更正为不限于 HashSet< T> ,其他集合,例如 Stack< T> Queue< T> 获得了这些改进。



关于任何遗漏原因的猜测尚无定论。可能是疏忽或时间压力,但坦率地说,这无关紧要。我怀疑即使我们喜欢相关轶事,即使Microsoft开发团队的直接投入也会有些主观。


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&amp; T&amp; gt;不能实现IReadOnlyCollection&lt; T&amp; gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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