Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Create a Dictionary of Generic Types?

Tags:

c#

generics

I'd like to create a Dictionary object, with string Keys, holding values which are of a generic type. I imagine that it would look something like this:

Dictionary<string, List<T>> d = new Dictionary<string, List<T>>();   

And enable me to add the following:

d.Add("Numbers", new List<int>()); d.Add("Letters", new List<string>()); 

I know that I can do it for a list of strings, for example, using this syntax:

Dictionary<string, List<string>> d = new Dictionary<string, List<string>>(); d.Add("Key", new List<string>()); 

but I'd like to do it for a generic list if possible...

2 questions then:

  1. Is it possible?
  2. What's the syntax?
like image 811
Jon Artus Avatar asked Mar 17 '09 15:03

Jon Artus


People also ask

Is dictionary a generic type?

The working of Dictionary is quite similar to the non-generic hashtable. The advantage of Dictionary is, it is generic type.

Can a dictionary have multiple types?

One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary.

How do you find a generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.

What is .NET dictionary?

Dictionary in . NET represents a collection of key/value pairs. In this tutorial, learn how to create a dictionary, add items to a dictionary, remove items from a dictionary, and other dictionary operations using C# and . NET. C# Dictionary class is a generic collection of keys and values pair of data.


1 Answers

EDIT: Now I've reread the question...

You can't do this, but a custom collection would handle it to some extent. You'd basically have a generic Add method:

public void Add<T>(string key, List<T> list) 

(The collection itself wouldn't be generic - unless you wanted to make the key type generic.)

You couldn't extract values from it in a strongly typed manner though, because the compiler won't know which type you've used for a particular key. If you make the key the type itself, you end with a slightly better situation, but one which still isn't supported by the existing collections. That's the situation my original answer was responding to.

EDIT: Original answer, when I hadn't quite read the question correctly, but which may be informative anyway...

No, you can't make one type argument depend on another, I'm afraid. It's just one of the things one might want to express in a generic type system but which .NET's constraints don't allow for. There are always going to be such problems, and the .NET designers chose to keep generics relatively simple.

However, you can write a collection to enforce it fairly easily. I have an example in a blog post which only keeps a single value, but it would be easy to extend that to use a list.

like image 132
Jon Skeet Avatar answered Oct 12 '22 02:10

Jon Skeet