// String._() 是一个扩展方法,用于连接前后对象的字符串形式。
public void ooo<T>(T t)
{
MessageBox.Show("1、"._(t)); }
public void ooo(params string[] s)
{
MessageBox.Show("2、"._(s));
}
private void button2_Click(object sender , EventArgs e)
{
ooo(new string[] { "1" , "2" , "3" });
}
很疑惑哦,为什么编译器会优先选择第2个?也就是显示“2、123”。只有显示指定ooo<string[]>(new string[] { "1" , "2" , "3" });才会匹配第一个。请问为什么?
public void ooo<T>(T t)
{
MessageBox.Show("1、"._(t)); }
public void ooo(params string[] s)
{
MessageBox.Show("2、"._(s));
}
private void button2_Click(object sender , EventArgs e)
{
ooo(new string[] { "1" , "2" , "3" });
}
很疑惑哦,为什么编译器会优先选择第2个?也就是显示“2、123”。只有显示指定ooo<string[]>(new string[] { "1" , "2" , "3" });才会匹配第一个。请问为什么?
如果你没有指定泛型类型参数,并且像楼主这种情况的话会
优先选择不是泛型的方法,因为
如果注掉
public void ooo(params string[] s)
{
MessageBox.Show("2、"._(s));
}
照样可以调用
ooo(new string[] { "1" , "2" , "3" });
指定了泛型参数那肯定就不用说了