经常看到类似下面的方法重载
虽然这样的重载能通过编译,但是会带来潜在的混乱和错误。当我们调用 Class1.Test(null) 时,我们并不能确定哪个方法会被执行,因此这样的编码设计是不可取的。还有下面的代码存在同样的问题。
public class Class1
{
public static void Test(string s)
{
}
public static void Test(object o)
{
}
}
{
public static void Test(string s)
{
}
public static void Test(object o)
{
}
}
虽然这样的重载能通过编译,但是会带来潜在的混乱和错误。当我们调用 Class1.Test(null) 时,我们并不能确定哪个方法会被执行,因此这样的编码设计是不可取的。还有下面的代码存在同样的问题。
public class Class1
{
public static void Test(object o)
{
}
public static void Test(params string[] ps)
{
}
}
{
public static void Test(object o)
{
}
public static void Test(params string[] ps)
{
}
}
public class Class1
{
public static void Test(int x, string s)
{
}
public static void Test(int x, object o)
{
}
}
{
public static void Test(int x, string s)
{
}
public static void Test(int x, object o)
{
}
}