I created a custom Dictionary class that inherits from a dictionary. However, strange things happen when calling the indexer, depending on how I use the class. Here's a simplified version of the class:
public class MyDictionary<TKey, TValue> : Dictionary<TKey, TValue> { public new TValue this[TKey key] { get { return base[key]; } set { base[key] = value; } } }
Now I want to create an instance and add something to it. The following works fine, i.e. I can set a breakpoint in the installer of the indexer and it will be deleted.
MyDictionary<int, string> dict = new MyDictionary<int, string>(); dict[0] = "some value";
However, if I do it like this (create an instance of the IDictionary variable):
IDictionary<int, string> dict = new MyDictionary<int, string>(); dict[0] = "some value";
it will no longer hit my breakpoint in the indexer installer, i.e. it should call something else. If I look at the implementation of the .NET Dictionary (from which my class inherits), I cannot find another indexer other than the one I redefine, and it does not inherit from anything else. So the question is what is going on?
source share