有时我会`2在堆栈跟踪中看到这一点.例如:
System.Collections.Generic.KeyNotFoundException:给定的键不在字典中.在System.Collections.Generic.Dictionary`2.get_Item(TKey key)
`2字典后是什么意思?
该System.Collections.Generic.Dictionary`2方法的类型System.Collections.Generic.Dictionary,有两个类型参数。所以在这种情况下,这意味着类型是System.Collections.Generic.Dictionary<TKey, TValue>,我们都知道。
这就是.Net创建类名称的方式.最初的声明
Dictionary<K, V>
Run Code Online (Sandbox Code Playgroud)
会变成Dictionary'2类型名称,其中'2意味着2个通用参数:
// Dictionary`2 - two generic parameters
Console.WriteLine(typeof(Dictionary<int, string>).Name);
// List`1 - one generic parameter
Console.WriteLine(typeof(List<int>).Name);
Run Code Online (Sandbox Code Playgroud)
请比较:
// IDictionary`2 - two generic parameters
Console.WriteLine(typeof(IDictionary<int, string>).Name);
// IDictionary - no generic parameters
Console.WriteLine(typeof(System.Collections.IDictionary).Name);
Run Code Online (Sandbox Code Playgroud)