C# 中的重载

csharpprogrammingserver side programming更新于 2025/8/13 15:37:17

C# 中的重载有两种类型。

函数重载

在同一作用域内,同一个函数名可以有多个定义。每个函数的定义必须在参数列表的类型和/或参数数量上有所不同。

我们来看一个例子 −

public static int mulDisplay(int one, int two) { }
public static int mulDisplay(int one, int two, int three) { }
public static int mulDisplay(int one, int two, int three, int four) { }

运算符重载

重载运算符是具有特殊名称的函数。关键字"operator"后跟所定义运算符的符号。

public static Box operator+ (Box b, Box c) {
   Box box = new Box();
   box.length = b.length + c.length;
   box.breadth = b.breadth + c.breadth;
   box.height = b.height + c.height;
   return box;
}

相关文章