C# 中重载方法有哪些不同的方式?

来源:这里教程网 时间:2026-02-21 16:25:57 作者:

c# 中重载方法有哪些不同的方式?

重载方法的不同方式是 -

The datatypes of parameters are different
The number of parameters are different

下面给出了一个示例,说明参数的不同数据类型 -

void print(int i) {
   Console.WriteLine("Printing int: {0}", i );
}
void print(double f) {
   Console.WriteLine("Printing float: {0}" , f);
}
void print(string s) {
   Console.WriteLine("Printing string: {0}", s);
}

下面列出了不同数量的参数 -

// two parameters
public static int mulDisplay(int one, int two) {
   return one * two;
}
// three parameters
public static int mulDisplay(int one, int two, int three) {
   return one * two * three;
}
// four parameters
public static int mulDisplay(int one, int two, int three, int four) {
   return one * two * three * four;
}

相关推荐