亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通用字典參數C#(從中返回隨機鍵)

通用字典參數C#(從中返回隨機鍵)

C#
手掌心 2021-04-09 17:15:37
是否可以具有一個接收通用字典參數并從中返回隨機鍵的函數?由于字典的“鍵”值可以是任何數據類型,因此我希望字典參數是通用的,無論哪種數據類型,它都將返回一個隨機鍵。到目前為止,我已經寫了這篇文章,但出現了一個錯誤。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 關注
  • 1095 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號