是否可以具有一個接收通用字典參數(shù)并從中返回隨機鍵的函數(shù)?由于字典的“鍵”值可以是任何數(shù)據(jù)類型,因此我希望字典參數(shù)是通用的,無論哪種數(shù)據(jù)類型,它都將返回一個隨機鍵。到目前為止,我已經(jīng)寫了這篇文章,但出現(xiàn)了一個錯誤。Dictionary<int, string> dict = new Dictionary<int, string>();dict.Add(1, "1003206");dict.Add(2, "1234567");dict.Add(3, "5432567");int randomKey = (int)RandomDictionaryKeyValue<Dictionary<int, string>>(dict);private T RandomDictionaryKeyValue<T>(Dictionary<T, T> dict) { List<T> keyList = new List<T>(dict.Keys); Random rand = new Random(); return keyList[rand.Next(keyList.Count)];}我收到錯誤消息:CS1503參數(shù)1:無法從轉(zhuǎn)換'System.Collections.Generic.Dictionary<int, string>'為'System.Collections.Generic.Dictionary<System.Collections.Generic.Dictionary<int, string>, System.Collections.Generic.Dictionary<int, string>>'我知道如何在列表中獲取Access隨機項,但是我不知道如何正確地將字典傳遞給我的方法。
- 3 回答
- 0 關(guān)注
- 1086 瀏覽
添加回答
舉報
0/150
提交
取消