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

運算子過載

過載的運算子是具有特殊名稱的函式。關鍵字運算子後緊跟被定義運算子的符號。

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 次瀏覽

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告