Dictionary`2 value in stack trace

Sometimes I see this `2 in the stack trace. For example:

System.Collections.Generic.KeyNotFoundException: this key was not in the dictionary. in System.Collections.Generic.Dictionary`2.get_Item (TKey key)

What is the meaning of `2 after the dictionary?

+7
stack-trace c #
source share
2 answers

System.Collections.Generic.Dictionary`2 means that the type is System.Collections.Generic.Dictionary with two type arguments. Thus, in this case, this means that the type System.Collections.Generic.Dictionary<TKey, TValue> , as we all know.

+4
source share

Thus .Net creates class names. Initial declaration

  Dictionary<K, V> 

the type name will be converted to Dictionary'2 , where '2 means two common parameters:

  // Dictionary`2 - two generic parameters Console.WriteLine(typeof(Dictionary<int, string>).Name); // List`1 - one generic parameter Console.WriteLine(typeof(List<int>).Name); 

Please compare:

  // IDictionary`2 - two generic parameters Console.WriteLine(typeof(IDictionary<int, string>).Name); // IDictionary - no generic parameters Console.WriteLine(typeof(System.Collections.IDictionary).Name); 
+5
source share

All Articles