Title is basic enough, why can’t I:
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.AddRange(MethodThatReturnAnotherDic());
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A comment to the original question sums this up pretty well:
As to why? Well, likely because the behavior of merging dictionaries can’t be reasoned about in a manner that fits with the Framework guidelines.
AddRangedoesn’t exist because a range doesn’t have any meaning to an associative container, as the range of data allows for duplicate entries. E.g if you had anIEnumerable<KeyValuePair<K,T>>that collection does not guard against duplicate entries.The behavior of adding a collection of key-value pairs, or even merging two dictionaries is straight-forward. The behavior of how to deal with multiple duplicate entries, however, is not.
What should be the behavior of the method when it deals with a duplicate?
There are at least three solutions I can think of:
When an exception is thrown, what should be the state of the original dictionary?
Addis almost always implemented as an atomic operation: it succeeds and updates the state of the collection, or it fails, and the state of the collection is left unchanged. AsAddRangecan fail due to duplicate errors, the way to keep its behavior consistent withAddwould be to also make it atomic by throwing an exception on any duplicate, and leave the state of the original dictionary as unchanged.As an API consumer, it would be tedious to have to iteratively remove duplicate elements, which implies that the
AddRangeshould throw a single exception that contains all the duplicate values.The choice then boils down to:
There are arguments for supporting both use cases. To do that, do you add a
IgnoreDuplicatesflag to the signature?The
IgnoreDuplicatesflag (when set to true) would also provide a significant speed up, as the underlying implementation would bypass the code for duplicate checking.So now, you have a flag that allows the
AddRangeto support both cases, but has an undocumented side effect (which is something that the Framework designers worked really hard to avoid).Summary
As there is no clear, consistent and expected behavior when it comes to dealing with duplicates, it’s easier to not deal with them all together, and not provide the method to begin with.
If you find yourself continually having to merge dictionaries, you can of course write your own extension method to merge dictionaries, which will behave in a manner that works for your application(s).