我有密码
//this runs
string[] s_items = {"0","0"};
string s_output = string.Format("{0}{1}",s_items);
//this one throws a exception
int[] i_items = {0,0};
string i_output = string.Format("{0}{1}",i_items);
为什么第一个运行而第二个抛出异常? 为什么选择
int[]
Format(String, Object)
过载
string[]
Format(String, Object[])
过载
最佳答案
string[]
可以转换为 object[]
,因为它们都是引用类型的数组。并且所有引用都是“相等的”。这是从第一天开始就内置到 C# 语言中的令人讨厌的(数组)转换之一,本不应该存在,但从第一天开始我们就没有泛型和适当的协变/逆变规则。
int[]
不能转换为 object[]
因为 int
,第一个数组中实际包含的东西,是不是引用。
关于c# - 正确的过载选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51280028/