Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โอเวอร์โหลดใน 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;
}