If it exists, what is the C# equivalent of the following Java code:
new HashMap<Class<? extends BaseClass>, Integer>();
I currently use new Dictionary<Type, int>(), which is more like
new HashMap<Class<?>, Integer>() which is obviously not the same.
(Ignore the differences between HashMap and Dictionary)
Edit: To clarify, I am not trying to define a new class, simply create an instance of HashMap/Dictionary.
There is no equivalent of the Java wildcard in C#. In Java, the type for types is
Class<T>whereTis the class itself. The equivalent in C# is the typeType, which is not generic. So it seems that the best you can do is to have, as you said, aDictionary<Type, int>, and if it’s encapsulated in a class you can restrict what you put in the dictionary in the code (so it will just be a runtime check):You can even implement your own
IDictionarywith that logic.UPDATE
Another runtime trick I can think of is to use a wrapper class for your types:
(Also implement
EqualsandGetHashCode, just delegate toType.)And then your dictionary becomes: