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();
Share:
35,448
Pratik Deoghare
Author by

Pratik Deoghare

Updated on July 12, 2022

Comments

  • Pratik Deoghare
    Pratik Deoghare almost 2 years

    I want all the functionality of Dictionary<TKey,TValue> but I want it as Foo<TKey,TValue>.
    How should I go about doing this?
    Currently I am using

    class 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
    Jon Skeet over 14 years
    Not within the constructors, no... they're already within the type, so they already "have" the type parameters.
  • ajithmanmu
    ajithmanmu over 14 years
    Nope, TKey and TValue are now defined as part of the class. You don't need to redefine them in each method.
  • Henk Holterman
    Henk Holterman over 14 years
    and make them public, usually.
  • Pratik Deoghare
    Pratik Deoghare over 14 years
    Sorry,I deleted the comment by accident.
  • Pratik Deoghare
    Pratik Deoghare over 14 years
    How can I make them all public without writing public in front of each one?
  • ajithmanmu
    ajithmanmu over 14 years
    public has to be explicitly defined in C#, without it everything defaults to private.
  • Pratik Deoghare
    Pratik Deoghare over 14 years
    I guess then I will have to override each and every constructor of the base class explicitly or is there any other way ?
  • Pratik Deoghare
    Pratik Deoghare over 14 years
    (+1) What do you recommend if I want to use just the Dictionary but with name Foo?
  • Stefan Steinegger
    Stefan Steinegger over 14 years
    Why would you do this? A kind of C-style typedef?
  • Polyfun
    Polyfun over 14 years
    Not quote everything defaults to private: class, struct and enum default to internal, whereas methods, properties and fields default to private.
  • The Chairman
    The Chairman over 14 years
    That'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
    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
    cowlinator over 8 years
    What is the point of inheriting from Dictionary if none of the methods are virtual?
  • MarioDS
    MarioDS about 8 years
    @TheChairman FWIW, if you're using ReSharper, there's a very handy context action that delegates all IDictionary members to the myDict field (when fixing the "missing members" error). You can also press alt+ins and choose "Delegating members..." to insert them. Saves a lot of work!
  • Alex Fainshtein
    Alex Fainshtein almost 2 years
    This 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 generic Dictionary<TKey, TValue>. And "Go to Declaration" seems plainly broken. Alas.