想知道是否可能有一個(gè)帶有字典參數(shù)的方法,其中TKey和TValue類(lèi)型是不確定的。例如,我可能要發(fā)送一個(gè)int的鍵和一個(gè)值作為自定義對(duì)象,或者發(fā)送一個(gè)字符串和另一個(gè)對(duì)象的值的鍵??梢酝ㄟ^(guò)通用對(duì)象完成此操作嗎,是重載的最佳實(shí)踐,還是我還沒(méi)有找到其他方法?public static SortedDictionary<??, ??> CreateSortedDictionary(Dictionary<??, ??> d){ SortedDictionary<??, ??> rv = new SortedDictionary<??, ??>(); enter code here... return rv;}
1 回答

慕桂英3389331
TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以將通用類(lèi)型用于鍵和值,然后將其用于輸入和輸出字典:
public static SortedDictionary<TKey, TValue> CreateSortedDictionary<TKey, TValue>(
Dictionary<TKey, TValue> input)
{
return new SortedDictionary<TKey, TValue>(input);
}
- 1 回答
- 0 關(guān)注
- 172 瀏覽
添加回答
舉報(bào)
0/150
提交
取消