I am trying to implement a MultiDictionary: a Dictionary which holds a List in each key.
I want to be able to add elements to the dictionary using the addition assignment operator and indexers :
myMultiDictionary[key] += elementToAdd;
I first created a custom AddableList which inherits from List and overloads the + and - operators, which let's me use the addition assignment operator :
myAddableList += elementToAdd;
MultiDictionary is then a simple inheritance of Dictionary<keyType, AddableList<contentType>>.
I'm having problems with the indexers. I wish to keep the Dictionary inheritance to use the O(1) performance of dictionary keys. However, I have to redefine the indexers to:
- create an empty list if I create a new key;
- delete a key if I remove all elements in the list.
How does inheritance work for classes where indexers are already defined? Will my new indexers overwrite all access methods defined in dictionary?