是否可以具有一个接收通用字典参数并从中返回随机键的函数?由于字典的“键”值可以是任何数据类型,因此我希望字典参数是通用的,无论哪种数据类型,它都将返回一个随机键。到目前为止,我已经写了这篇文章,但出现了一个错误。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参数1:无法从转换'System.Collections.Generic.Dictionary<int, string>'为'System.Collections.Generic.Dictionary<System.Collections.Generic.Dictionary<int, string>, System.Collections.Generic.Dictionary<int, string>>'我知道如何在列表中获取Access随机项,但是我不知道如何正确地将字典传递给我的方法。
3 回答
- 3 回答
- 0 关注
- 1067 浏览
添加回答
举报
0/150
提交
取消