Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 597899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:19:50+00:00 2026-05-13T16:19:50+00:00

Okay – so I know it’s simple to build a factory method that provides

  • 0

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)?

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.

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 🙂

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.

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:19:51+00:00Added an answer on May 13, 2026 at 4:19 pm

    Totally unofficial guess:

    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);
    

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I know that using eval() isn't great, but I haven't been able
Okay i know i have to use the JDBC etc, but im not sure
Okay, we know that the following two lines are equivalent - (0 == i)
Okay, I have a method that takes the variables: an image, int x, int
Okay, next PHPExcel question. I have an HTML form that users fill out and
Okay this question is very simple: I have a facebook page, and a website.
Okay, I feel a bit foolish for having to ask this but I guess
okay, i'm setting up a multi-user chat system. i have a messages table, that
Okay, this is probably a very basic question; but, I'm just getting back in
Okay, so this is a long long shot but here it goes... When IE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.