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:
How does inheritance work for classes where indexers are already defined? Will my new indexers overwrite all access methods defined in dictionary?
Since the indexer of Dictionary is not marked as virtual, you cannot really override it. You can only hide it by adding a 'new' keyword to your indexer, but don't do this, anybody could upcast your MultiDictionary to Dictionary and use the indexer of the base class. Best solution for you is, rather than inherit from a Dictionary, your MultiDictionary should implement an interface IDictionary, than hold private Dictionary instance internally in MultiDictionary and redirect all interface implementations without any custom logic to the Dictionary instance. In other words just create Proxy to the Dictionary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With