public static OrderedDictionary Drinks = new OrderedDictionary();
Drinks["Water"] = 3;
Drinks["Coffee"] = 2
Drinks["Beer"] = 5;
如何根據位置獲取物品名稱? 我想指定位置1來獲得「水」。如何按位置獲取物品名稱OrderDirectory
public static OrderedDictionary Drinks = new OrderedDictionary();
Drinks["Water"] = 3;
Drinks["Coffee"] = 2
Drinks["Beer"] = 5;
如何根據位置獲取物品名稱? 我想指定位置1來獲得「水」。如何按位置獲取物品名稱OrderDirectory
試試這個:
對於值:
var result= (Drinks.Cast<DictionaryEntry>().ElementAt(2)).Value;
爲重點
var result= (Drinks.Cast<DictionaryEntry>().ElementAt(2)).Key;
關閉。 OP會喜歡'Key',而不是'Value'。除此之外,這是按要求工作 - http://ideone.com/rmAU1h – CodingIntrigue
試試這個,
var result = Drinks.Keys.OfType<string>().ElementAt(0);
這將返回OrderedDictionary
第一個關鍵。在這裏你確定你的密鑰類型是字符串。但如果你是關鍵的不確定數據類型,然後使用object
一樣,
var result = Drinks.Keys.OfType<object>().ElementAt(0);
非常感謝工作:D – Do0med
歡迎:) –
試試這個http://stackoverflow.com/questions/2229951/how-to-get-key-from-ordereddictionary -in-c-sharp-by-index – spajce
如果你想使用順序索引訪問數據,爲什麼要使用字典?在什麼情況下可以將'1'歸還'水'? – CodingIntrigue
我不確定你要完成什麼......你爲什麼要使用有序字典,你想做什麼?如果我們知道更多,我們可以幫助你更好 – KapteinMarshall