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

C# - 메서드

메소드는 작업을 함께 수행하는 명령문의 그룹입니다. 모든 C# 프로그램에는 Main이라는 메서드가 있는 클래스가 하나 이상 있습니다.

방법을 사용하려면 다음을 수행해야 합니다.

C#에서 메소드 정의

메소드를 정의할 때 기본적으로 해당 구조의 요소를 선언합니다. C#에서 메서드를 정의하는 구문은 다음과 같습니다. -

<Access Specifier> <Return Type> <Method Name>(Parameter List) {
   Method Body
}

다음은 메소드의 다양한 요소입니다 -

다음 코드 스니펫은 FindMax 기능을 보여줍니다. 두 개의 정수 값을 취하고 둘 중 더 큰 값을 반환합니다. 공개 액세스 지정자가 있으므로 클래스의 인스턴스를 사용하여 클래스 외부에서 액세스할 수 있습니다.

class NumberManipulator {

   public int FindMax(int num1, int num2) {
      /* local variable declaration */
      int result;

      if (num1 > num2)
         result = num1;
      else
         result = num2;

      return result;
   }
   ...
}

C#에서 메소드 호출

메서드 이름을 사용하여 메서드를 호출할 수 있습니다. 다음 예는 이것을 보여줍니다 -

라이브 데모
using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int FindMax(int num1, int num2) {
         /* local variable declaration */
         int result;
         
         if (num1 > num2)
            result = num1;
         else
            result = num2;
         return result;
      }
      
      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator();

         //calling the FindMax method
         ret = n.FindMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

위의 코드를 컴파일하고 실행하면 다음과 같은 결과가 생성됩니다. -

Max value is : 200

클래스의 인스턴스를 사용하여 다른 클래스에서 public 메서드를 호출할 수도 있습니다. 예를 들어 FindMax 메소드는 NumberManipulator에 속합니다. 다른 클래스에서 호출할 수 있습니다. Test .

라이브 데모
using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int FindMax(int num1, int num2) {
         /* local variable declaration */
         int result;
         
         if(num1 > num2)
            result = num1;
         else
            result = num2;
         
         return result;
      }
   }
   class Test {
      static void Main(string[] args) {
         /* local variable definition */
         int a = 100;
         int b = 200;
         int ret;
         NumberManipulator n = new NumberManipulator();
         
         //calling the FindMax method
         ret = n.FindMax(a, b);
         Console.WriteLine("Max value is : {0}", ret );
         Console.ReadLine();
      }
   }
}

위의 코드를 컴파일하고 실행하면 다음과 같은 결과가 생성됩니다. -

Max value is : 200

재귀 메서드 호출

메서드는 자신을 호출할 수 있습니다. 이것을 재귀라고 합니다. . 다음은 재귀 함수 −

를 사용하여 주어진 숫자에 대한 계승을 계산하는 예입니다. 라이브 데모
using System;

namespace CalculatorApplication {
   class NumberManipulator {
      public int factorial(int num) {
         /* local variable declaration */
         int result;
         if (num == 1) {
            return 1;
         } else {
            result = factorial(num - 1) * num;
            return result;
         }
      }
      static void Main(string[] args) {
         NumberManipulator n = new NumberManipulator();
         //calling the factorial method {0}", n.factorial(6));
         Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
         Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
         Console.ReadLine();
      }
   }
}

위의 코드를 컴파일하고 실행하면 다음과 같은 결과가 생성됩니다. -

Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320

메서드에 매개변수 전달

매개변수가 있는 메서드가 호출되면 매개변수를 메서드에 전달해야 합니다. 매개변수가 메소드에 전달될 수 있는 세 가지 방법이 있습니다 -

시니어 번호 메커니즘 및 설명
1 값 매개변수

이 메서드는 인수의 실제 값을 함수의 형식 매개변수에 복사합니다. 이 경우 함수 내부의 매개변수에 대한 변경 사항은 인수에 영향을 미치지 않습니다.

2 참조 매개변수

이 메서드는 인수의 메모리 위치에 대한 참조를 형식 매개변수에 복사합니다. 이는 매개변수에 대한 변경 사항이 인수에 영향을 미친다는 것을 의미합니다.

3 출력 매개변수

이 메서드는 둘 이상의 값을 반환하는 데 도움이 됩니다.


C 언어

  1. C# 추상 클래스 및 메서드
  2. C# 부분 클래스 및 부분 메서드
  3. C# 봉인된 클래스 및 메서드
  4. C# 메서드 오버로딩
  5. C - 기능
  6. C - 비트 필드
  7. C - 변수 인수
  8. 자바 8 - 기본 메소드
  9. MATLAB - 통합
  10. C# - 대리자