sudhavamsikiran
sudhavamsikiran

Reputation: 11

Need how to inherit a dictionary class with template type

I got hanged with the problem. I have a class which will inherit dictionary.But dictionary should be of template type. I got succeeded till inheriting a dictionary class to a class. But am not getting how make derived dictionary as a template. Please help in this. Please get my code below.

public class MGDDictionary<TKey, TValue> : Dictionary<string,string>
    {
        public MGDDictionary()
        {

        }

        public static SelectedOption value { get; set; }

        public override string ToString()
        {
            return EntitySerializer.ObjToString<MGDDictionary<TKey, TValue>>(serializer, this);
        }
        public static MGDDictionary<TKey, TValue> FromString(string objectStream)
        {
            return EntitySerializer.FromString<MGDDictionary<TKey,TValue>>(serializer,objectStream);
        }
    }

In the above code I need this line to be of a class which inherits dictionary with a template in it.

public class MGDDictionary<TKey, TValue> : Dictionary<string,string>

Many more Thanks in advance

Upvotes: 1

Views: 4799

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500225

It makes no sense for your class to be generic. I think you can just change it to remove the type parameters:

public class MGDDictionary : Dictionary<string,string>
{
    public MGDDictionary()
    {

    }

    public static SelectedOption value { get; set; }

    public override string ToString()
    {
        return EntitySerializer.ObjToString<MGDDictionary>(serializer, this);
    }
    public static MGDDictionary FromString(string objectStream)
    {
        return EntitySerializer.FromString<MGDDictionary>(serializer,objectStream);
    }
}

On the other hand, creating your own collection classes is rarely a good idea. Prefer composition over inheritance, in general.

Also note that a static property called value is almost certainly a bad idea, both in the name and the staticness...

Upvotes: 2

Related Questions