为什么不字典< TKEY的,TValue>有一个IEnumerable< KeyValuePair< TKEY的,TValue>>构造函数? [英] Why doesn't Dictionary<TKey, TValue> have an IEnumerable<KeyValuePair<TKey, TValue>> ctor?

查看:206
本文介绍了为什么不字典< TKEY的,TValue>有一个IEnumerable< KeyValuePair< TKEY的,TValue>>构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了 - 所以我知道这是简单的建立一个工厂方法,它提供的功能;但鉴于词典< TKEY的,TValue> 的IEnumerable< KeyValuePair< TKEY的,TValue>> ,不应该它有一个构造函数等同于,例如,名单,其中,T> 的ctor(IEnumerable的< T>范围)

Okay - so I know it's simple to build a factory method that provides the functionality; but given that Dictionary<TKey, TValue> is IEnumerable<KeyValuePair<TKey, TValue>>, shouldn't it have a Ctor equivalent to, for example, List<T>'s ctor(IEnumerable<T> range)?

它甚至愚蠢因为它提供了一个构造函数,它接受一个的IDictionary&LT; TKEY的,TValue&GT; 为源,但由于该接口也 IEnumerable的&LT; KeyValuePair&LT; TKEY的,TValue&GT;&GT; ,了IEnumerable的选择肯定会更有意义;除非的IEnumerable&LT;&GT; 接口不在身边的第一类设计的时候

It's even sillier given that it provides a Ctor that takes an IDictionary<TKey, TValue> as a source, but since that interface is also IEnumerable<KeyValuePair<TKey, TValue>>, the IEnumerable option would surely have made more sense; unless the IEnumerable<> interface wasn't around when the class first designed.

据得到的更坏,因为如果你的IDictionary的版本的构造函数的实现 - 输入字典导入有以下code:

It get's worse because if you look at the implementation of the IDictionary version of the ctor - the input dictionary is imported with the following code:

foreach (KeyValuePair<TKey, TValue> pair in dictionary)
{
    this.Add(pair.Key, pair.Value);
}

任何人都想到了一个很好的理由,为什么架构设计师选择了当一个基本接口是所有被要求的最具体的接口?

Anyone think of a good reason for why the framework designers chose the most specific interface when a base interface was all that was required?

修改

Edit

@马克西曼认为,这是为了避免异常被提出的重复键 - 这可能是接近真理 - 但考虑这个例子:

@Mark Seeman suggests that it's to avoid Exceptions being raised by duplicate keys - which is probably close to the truth - but consider this example:

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void How_To_Break_The_IDictionary_Ctor_Design_Decision()
{
  Dictionary<string, string> dictionary = new Dictionary<string, string>();
  dictionary.Add("hello", "world");
  dictionary.Add("Hello", "world");

  Dictionary<string, string> dictionary2 = 
    new Dictionary<string, string>(dictionary,
                                   StringComparer.CurrentCultureIgnoreCase);
}

我知道 - 测试的反 - 但由于某些原因,我认为这使我好点:)

I know - the test's in reverse - but for some reason I thought it made my point better :)

鉴于键比较不IDictionary接口的一部分,你可以的永远的保证,要导入的词典不会产生重复键,因此一个的ArgumentException ,在构建新的。

Given that the Key comparer is not part of the IDictionary interface, you can never guarantee that the dictionary you're importing will not generate duplicate keys, and therefore an ArgumentException, in constructing the new one.

人机工程学 - 你可能也只是有一个IEnumerable构造做同样的事情

Ergo - you might as well just have an IEnumerable constructor that does the same thing.

推荐答案

完全非官方的猜测

如果一个构造函数允许一个的IEnumerable&LT; KeyValuePair&LT; TKEY的,TValue&GT;&GT; 您已经能够提供使用相同的密钥多个项目,这将是预期对这种行为?

If a constructor allowed an IEnumerable<KeyValuePair<TKey, TValue>> you would have been able to supply multiple items with the same key and what would be the expected behavior of that?

例如。你可以做这样的事情:

E.g. you could have done something like this:

var kvps = new[]
{
    new KeyValuePair<int, string>(1, "Foo"),
    new KeyValuePair<int, string>(1, "Bar"),
    new KeyValuePair<int, string>(1, "Baz")        
}
var dic = new Dictionary<int, string>(kvps);

您可能会说,这应该只是抛出一个异常要与Add方法的行为是一致的,但我猜想,设计团队认为这将是混乱的一个更大的来源不是真正有用的...

You could argue that this should simply throw an exception to be consistent with the behavior of the Add method, but I would guess that the design team thought it would be a greater source of confusion than actually useful...

这篇关于为什么不字典&LT; TKEY的,TValue&GT;有一个IEnumerable&LT; KeyValuePair&LT; TKEY的,TValue&GT;&GT;构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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