Search code examples
c#genericsinstantiation

Instantiating of a generic collection class c#


learn.microsoft.com: "Because a generic type definition is only a template, you cannot create instances of a class, structure, or interface that is a generic type definition." so how come I can write this: Dictionary<int, string> d = new Dictionary<int, string>(); or it is not an instantiation? so what am I doing then?


Solution

  • It is an instanciation. When you do Dictionary<int, string> d = new Dictionary<int, string>(), you instanicate Dictionary<TKey,TValue> with TKey : int and TValue : string.

    What you cannot do is Dictionary d = new Dictionary() which should return the error you are describing.