pra*_*mna 5 c# dictionary ordereddictionary key-value
我有一个 OrderedDictionary,如下面的代码片段所示,其中的键是 strings、integer 和 character。
好吧,我对 OrderedDictionary 很陌生,我所知道的是有序字典可以存储任何类型的键/值对,我们可以通过索引和键访问值。
OrderedDictionary od = new OrderedDictionary();
od.Add("Key1", "Val1");
od.Add("Key2", "Val2");
od.Add("Key3", "Val3");
od.Add(1, "Val4");
od.Add('k', 'V');
Run Code Online (Sandbox Code Playgroud)
所以,我想知道我是否需要访问上面的Val4,那我应该怎么做?因为当我尝试使用
Console.WriteLine(od[1]);
Run Code Online (Sandbox Code Playgroud)
它给了我“Val2”,因为它显然正在考虑将“1”作为索引。
非常感谢!
您可以将整数值 1 转换为 object 以命中正确的索引器。
Console.WriteLine(od[(object)1]);
Run Code Online (Sandbox Code Playgroud)