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.