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

  • SEARCH
  • Home
  • 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 6376517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:45:22+00:00 2026-05-25T01:45:22+00:00

I wanted to create a Dictionary-like object and thought the correct way would be

  • 0

I wanted to create a Dictionary-like object and thought the correct way would be to implement the IDictionary<K,V> interface, and use composition to include the underlying dictionary. I began with the below (K=string, V=int)

public class DictionaryLikeObject : IDictionary<string,int> {
  Dictionary<string,int> _backingDictionary = new Dictionary<string,int>();
}

Then I used Visual Studio’s “Implement Interface” ability to stub out all the cover methods that I would need.

Three methods of IDictionary do not seem to exist in Dictionary:

void Add(KeyValuePair<string, int> item);
void CopyTo(KeyValuePair<string, int>[] array, int arrayIndex);
bool Remove(KeyValuePair<string, int> item);

Yet the Microsoft documentation clearly indicates that Dictionary implements IDictionary. So I would have expected these three methods to be available. To copy from the documentation, the definition of Dictionary<K,V>

[SerializableAttribute]
[ComVisibleAttribute(false)]
public class Dictionary<K, V> : IDictionary<K, V>, 
ICollection<KeyValuePair<K, V>>, IEnumerable<KeyValuePair<K, V>>, 
IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

These three missing methods, I believe, are found in ICollection<>. But so are other methods such as Clear() that Dictionary does have.

Question 1: How can C# get away without implementing these three, and why is this so? I suspect this is a compiler error (for my reasoning, see below).
Question 2: Alternatively, what am I missing?

Here’s why I think it might be a compiler error. Examine the following code:

Dictionary<string, int> dictionary1 = new Dictionary<string, int>();
IDictionary<string, int> dictionary2 = new Dictionary<string, int>();
KeyValuePair<string, int> item = new KeyValuePair<string, int>("test", 1);
//dictionary1.Add(item); // compile error: No overload for method 'Add' takes 1 argument
dictionary2.Add(item); // works like a charm
Debug.WriteLine(@"dictionary2[""test""] = {0}", dictionary2["test"]); // outputs: dictionary2["test"] = 1

The method void Add(KeyValuePair<string, int> item) appears not to be in Dictionary<string,int> (since it doesn’t compile), but it is in IDictionary<string,int>, and somehow the compiler does properly find an implementation of it. Question 3: What’s going on?

Note that the Microsoft documentation for Dictionary<K,V> does not specify these three methods.

Lastly, in my actual implementation, I ended up using

IDictionary<string,int> _backingDictionary = new Dictionary<string,int>();

instead of

Dictionary<string,int> _backingDictionary = new Dictionary<string,int>();

so that all three methods could easily work.

  • 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-25T01:45:23+00:00Added an answer on May 25, 2026 at 1:45 am

    The Dictionary<TKey, TValue> does implement these methods, it just does so explicitly. Hence you must access it via the IDictionary<TKey, TValue> interface.

    Dictionary<string, string> map = ...;
    KeyValuePair<string, string> pair = ...;
    map.Add(pair);  // Compilation Error
    ((IDictionary<string, string>)map).Add(pair);  // Works
    

    Explicit implementation works by specifying precisely which interface method an instance method implements at the point of definition. For example

    interface IFoo {
      void Method(); 
    }
    
    class C1 : IFoo {
      // Implicitly implements IFoo.Method
      public void Method() { }
    }
    
    class C2 : IFoo {
      // Explicitly implements IFoo.Method
      void IFoo.Method() { }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to create my own Python exception class, like this: class MyException(BaseException): def
I wanted to create a function which would return the path of the current
I wanted to create a custom combo box like this (as in MS Word),
I wanted to create a throwaway struct object to keep various status flags. My
I wanted to create an IDN-aware formencode validator to use in one of my
I wanted to create a view that looked something like the following but i
I wanted to create a string padding function for the use of left-padding a
I would like to implement what I know as a CVAR System, I'm not
I wanted to do something ‘dynamic’ with a dictionary object and supplied command line
I wanted to create an HTML page which would have a set search criterias.

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.