이름은 같지만 매개변수가 다른 두 개 이상의 메서드를 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) { } 다음은 메소드 오버로딩을 구현하는 방법을 보여주는 예입니다 -
예
using System;
public class Demo {
public static int mulDisplay(int one, int two) {
return one * two;
}
public static int mulDisplay(int one, int two, int three) {
return one * two * three;
}
public static int mulDisplay(int one, int two, int three, int four) {
return one * two * three * four;
}
}
public class Program {
public static void Main() {
Console.WriteLine("Multiplication of two numbers: "+Demo.mulDisplay(10, 15));
Console.WriteLine("Multiplication of three numbers: "+Demo.mulDisplay(8, 13, 20));
Console.WriteLine("Multiplication of four numbers: "+Demo.mulDisplay(3, 7, 10, 7));
}
} 출력
Multiplication of two numbers: 150 Multiplication of three numbers: 2080 Multiplication of four numbers: 1470