在c#中同样的方法名称不一样的参数数量和类型可以实现方法重载
class ResultDisplayer
{
void DisplayResult(string result)
{
// implementation
}
void DisplayResult(int result)
{
// implementation
}
}
再如
class MyClass
{
int DoSomething(int x) // want 2nd parameter with default value 10
{
DoSomething(x, 10);
}
int DoSomething(int x, int y)
{
// implementation
}
}
注意在C#中下面两种情况并不能实现重载
➤
It is not sufficient for two methods to differ only in their return type.
➤
It is not sufficient for two methods to differ only by virtue of a parameter having been declared as ref
or out
本文详细解析了C#中的方法重载特性,包括通过不同的参数数量和类型来实现方法的重载,同时指出了不能仅通过返回类型或参数的ref/out属性来区分方法的情况。

955

被折叠的 条评论
为什么被折叠?



