How do I inherit from Dictionary?
35,448
Solution 1
You were close, you just need to remove the type parameters from the constructors.
class Foo<TKey,TValue> : Dictionary<TKey, TValue>
{
Foo():base(){}
Foo(int capacity):base(capacity){}
}
To override a method you can use the override keyword.
Solution 2
Not directly answering your question, just an advice. I would not inherit the dictionary, I would implement IDictionary<T,K>
and aggregate a Dictionary. It is most probably a better solution:
class Foo<TKey,TValue> : IDictionary<TKey, TValue>
{
private Dictionary<TKey, TValue> myDict;
// ...
}
Solution 3
If you just want the same type but with a different name, you can shorten it with using
alias:
using Foo = System.Collections.Generic.Dictionary<string, string>;
and then
Foo f = new Foo();
Author by
Pratik Deoghare
Updated on July 12, 2022Comments
-
Pratik Deoghare almost 2 years
I want all the functionality of
Dictionary<TKey,TValue>
but I want it asFoo<TKey,TValue>
.
How should I go about doing this?
Currently I am usingclass Foo<TKey,TValue> : Dictionary<TKey, TValue> { /* I'm getting all sorts of errors because I don't know how to overload the constructors of the parent class. */ // overloaded methods and constructors goes here. Foo<TKey,TValue>():base(){} Foo<TKey,TValue>(int capacity):base(capacity){} }
What is the right way to overload constructors and methods of the parent class?
NOTE:I think I have misused the word 'overload' please correct it or suggest correction.
-
Jon Skeet over 14 yearsNot within the constructors, no... they're already within the type, so they already "have" the type parameters.
-
ajithmanmu over 14 yearsNope, TKey and TValue are now defined as part of the class. You don't need to redefine them in each method.
-
Henk Holterman over 14 yearsand make them public, usually.
-
Pratik Deoghare over 14 yearsSorry,I deleted the comment by accident.
-
Pratik Deoghare over 14 yearsHow can I make them all public without writing public in front of each one?
-
ajithmanmu over 14 yearspublic has to be explicitly defined in C#, without it everything defaults to private.
-
Pratik Deoghare over 14 yearsI guess then I will have to override each and every constructor of the base class explicitly or is there any other way ?
-
Pratik Deoghare over 14 years(+1) What do you recommend if I want to use just the Dictionary but with name Foo?
-
Stefan Steinegger over 14 yearsWhy would you do this? A kind of C-style typedef?
-
Polyfun over 14 yearsNot quote everything defaults to private: class, struct and enum default to internal, whereas methods, properties and fields default to private.
-
The Chairman over 14 yearsThat's a Decorator (en.wikipedia.org/wiki/Decorator_pattern). Possible drawback: You need to implement each connection from your class to your member dictionary manually.
-
Stefan Steinegger over 14 years@Mao: that's true, you need to implement each method, but you also might find it practical to have full control.
-
cowlinator over 8 yearsWhat is the point of inheriting from Dictionary if none of the methods are virtual?
-
MarioDS about 8 years@TheChairman FWIW, if you're using ReSharper, there's a very handy context action that delegates all
IDictionary
members to themyDict
field (when fixing the "missing members" error). You can also pressalt
+ins
and choose "Delegating members..." to insert them. Saves a lot of work! -
Alex Fainshtein almost 2 yearsThis is true and will work. But unfortunately, Visual Studio does not provide good support for aliasing. Right-clicking
Foo
in the code and selecting "Go to Definition" will bring you to the definition of genericDictionary<TKey, TValue>
. And "Go to Declaration" seems plainly broken. Alas.