In C#: How to declare a generic Dictionary whose key and value types have a common constraint type?
Posted
by Marcel
on Stack Overflow
See other posts from Stack Overflow
or by Marcel
Published on 2010-05-31T14:56:02Z
Indexed on
2010/05/31
15:03 UTC
Read the original article
Hit count: 171
Hi all,
I want to declare a dictionary that stores typed IEnumerable
's of a specific type, with that exact type as key, like so:
private IDictionary<T, IEnumerable<T>> _dataOfType where T: BaseClass; //does not compile!
The concrete classes I want to store, all derive from BaseClass, therefore the idea to use it as constraint. The compiler complains that it expects a semicolon after the member name.
If it would work, I would expect this would make the later retrieval from the dictionary simple like:
IEnumerable<ConcreteData> concreteData;
_sitesOfType.TryGetValue(typeof(ConcreteType), out concreteData);
How to define such a dictionary?
© Stack Overflow or respective owner