C# 中的重载


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) { }

运算符重载

重载的运算符是具有特殊名称的函数。keyword 运算符后跟要定义的运算符符号。

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;
}

更新于:2020 年 6 月 21 日

251 次浏览

开启您的 职业生涯

完成课程认证

开始
广告
© . All rights reserved.