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

연산자 과부하

오버로드된 연산자는 특별한 이름을 가진 함수입니다. 키워드 operator 다음에 정의되는 연산자에 대한 기호가 옵니다.

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