c# 方法重载

在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

原文地址:https://www.cnblogs.com/tianmochou/p/5086434.html