산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
home  MfgRobots >> 산업 제조 >  >> Industrial programming >> C 언어

C# 메서드 오버로딩

C# 메소드 오버로딩

이 문서에서는 예제를 통해 C#의 메서드 오버로딩에 대해 알아봅니다.

C#에서는 이름은 같지만 매개변수의 번호, 유형 및 순서가 다른 두 개 이상의 메서드가 있을 수 있으며 이를 메서드 오버로딩이라고 합니다. 예:

void display() { ... }
void display(int a) { ... }
float display(double a) { ... }
float display(int a, float b) { ... }

여기서 display() 메서드가 오버로드되었습니다. 이 메서드는 이름은 같지만 인수가 다릅니다.

참고 :위 메소드의 리턴 유형이 동일하지 않습니다. 메서드 오버로딩이 반환 형식과 관련이 없기 때문입니다. 오버로드된 메서드는 반환 유형이 같거나 다를 수 있지만 매개변수는 달라야 합니다.

<시간>

다음과 같은 방법으로 메서드 오버로딩을 수행할 수 있습니다.

1. 매개변수 수 변경

메소드의 매개변수 수가 다른 경우 메소드를 오버로드할 수 있습니다.

void display(int a) {
  ...
} 
...
void display(int a, int b) {
  ...
} 

여기에서는 동일한 이름을 가진 클래스에 두 개의 메소드가 있습니다 - display() . 메소드의 매개변수 수가 다르기 때문에 동일한 이름을 가진 메소드가 두 개 이상 있을 수 있습니다. 예를 들어,

using System;

namespace MethodOverload {

  class Program {  

    // method with one parameter
    void display(int a) {
      Console.WriteLine("Arguments: " + a);
    }
 
    // method with two parameters
    void display(int a, int b) {
      Console.WriteLine("Arguments: " + a + " and " + b);
    } 
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100);
      p1.display(100, 200);
      Console.ReadLine();
    }
  }
}

출력

Arguments: 100
Arguments: 100 and 200

위의 예에서 우리는 display()를 오버로드했습니다. 방법:

메소드 호출 시 전달된 인수의 수에 따라 해당 메소드가 호출됩니다.

<시간>

2. 매개변수의 데이터 유형을 변경하여

void display(int a) {
  ...
} 
...
void display(string b) {
  ...
} 

여기에는 display()의 두 가지 메서드가 있습니다. 같은 수의 매개변수로 둘 이상의 display()를 가질 수 있습니다. 메소드에서 매개변수의 데이터 유형이 다르기 때문에 동일한 수의 매개변수를 갖는 메소드입니다. 예를 들어,

using System;

namespace MethodOverload {

  class Program {  

    // method with int parameter
    void display(int a) {
      Console.WriteLine("int type: " + a);
    } 

    // method with string parameter
    void display(string b) {
      Console.WriteLine("string type: " + b);
    } 
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100);
      p1.display("Programiz");
      Console.ReadLine();
    }
  }
}

출력

int type: 100
string type: Programiz

위의 프로그램에서 display()를 오버로드했습니다. 다양한 유형의 매개변수가 있는 메소드

메소드 호출 시 전달된 인수의 유형에 따라 해당 메소드가 호출됩니다.

<시간>

3. 매개변수의 순서를 변경하여

void display(int a, string b) {
  ...
} 
...
void display(string b, int a) {
  ...
}

여기에는 display()의 두 가지 메서드가 있습니다. . 둘 이상의 display()를 가질 수 있습니다. 메소드에서 매개변수의 데이터 유형 순서가 다르기 때문에 동일한 수와 유형의 매개변수를 갖는 메소드입니다. 예를 들어,

using System;

namespace MethodOverload {

  class Program {  

    // method with int and string parameters 
    void display(int a, string b) {
      Console.WriteLine("int: " + a);
      Console.WriteLine("string: " + b);
    } 

    // method with string andint parameter
    void display(string b, int a) {
      Console.WriteLine("string: " + b);
      Console.WriteLine("int: " + a);
    } 
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100, "Programming");
      p1.display("Programiz", 400);
      Console.ReadLine();
    }
  }
}

출력

int: 100
string: Programming
string: Programiz
int: 400

위의 프로그램에서 display()를 오버로드했습니다. 매개변수 순서가 다른 메소드

메서드 호출 시 전달된 인수의 순서에 따라 해당 메서드가 호출됩니다.


C 언어

  1. C# 추상 클래스 및 메서드
  2. C# 부분 클래스 및 부분 메서드
  3. C# 봉인된 클래스 및 메서드
  4. C# 생성자 오버로딩
  5. 파이썬 연산자 오버로딩
  6. Verilog 작업
  7. Verilog 디스플레이 작업
  8. C# - 메서드
  9. C# - 연산자 오버로딩
  10. C# - 익명 메서드