想知道是否可能有一个带有字典参数的方法,其中TKey和TValue类型是不确定的。例如,我可能要发送一个int的键和一个值作为自定义对象,或者发送一个字符串和另一个对象的值的键。可以通过通用对象完成此操作吗,是重载的最佳实践,还是我还没有找到其他方法?public static SortedDictionary<??, ??> CreateSortedDictionary(Dictionary<??, ??> d){ SortedDictionary<??, ??> rv = new SortedDictionary<??, ??>(); enter code here... return rv;}
1 回答
![?](http://img1.sycdn.imooc.com/545865890001495702200220-100-100.jpg)
慕桂英3389331
TA贡献2036条经验 获得超8个赞
您可以将通用类型用于键和值,然后将其用于输入和输出字典:
public static SortedDictionary<TKey, TValue> CreateSortedDictionary<TKey, TValue>(
Dictionary<TKey, TValue> input)
{
return new SortedDictionary<TKey, TValue>(input);
}
- 1 回答
- 0 关注
- 157 浏览
添加回答
举报
0/150
提交
取消