You are not even trying to write anything generic. Where you use "<>" syntax you do something just the opposite — instantiate generic class. The class
System.Collections.Generic.Dictionary
is generic, the class
System.Collections.Generic.Dictionary<string,>
is not.
You really need to learn syntax and the idea of generics.
In your case, you need something like:
class MyClass<KEY, VALUE> {
internal MyClass(Dictionary<KEY, VALUE> dictionary) {
this.Dictionary = dictionary;
}
Dictionary<KEY, VALUE> Dictionary;
}
Dictionary<KEY, VALUE> dictionary = new Dictionary<KEY, VALUE>();
MyClass<string, string> instance = new MyClass<string, string>(dictionary);
Better yet, think about instantiating of the dictionary inside class. Is there any reason to use external dictionary?
Also, remember that your dictionary is of the reference type (a class). If you pass the same dictionary to two different instances of
MyClass
, adding an element in one instance will change the dictionary in another one because they reference the same
Dictionary
object. Do you really need that?
You can construct it where it is declared. You won't need a constructor with this parameter then.
So, if you change your mind, it will be:
class MyClass<KEY, VALUE> {
Dictionary<KEY, VALUE> Dictionary = new Dictionary<KEY, VALUE>();
}
MyClass<string, string> instance = new MyClass<string, string>();
—SA