>  기사  >  백엔드 개발  >  C# 작업 대리자

C# 작업 대리자

WBOY
WBOY원래의
2024-09-03 15:31:39833검색

내장 대리자는 시스템 네임스페이스에 정의된 일반 유형에 속합니다. 반환 값이 없는 메서드에서 이를 사용할 수 있습니다. 즉, 해당 메서드를 작업 대리자라고 합니다. 작업 대리자는 최소 1개의 입력 매개변수와 최대 16개의 입력 매개변수를 포함할 수 있으며, 이러한 매개변수는 동일한 데이터 유형이거나 다른 데이터 유형을 가질 수 있습니다. 프로그램에서 작업 대리자를 사용하면 프로그램이 더 최적화되고 읽기 쉬워집니다. 이번 주제에서는 C# Action Delegate에 대해 알아보겠습니다.

C#에서 Action Delegate의 구문은 다음과 같습니다.

public delegate void Action < in input_parameter_type > (input_parameter_type   object);
public delegate void Action < in input_parameter_type1, in input_parameter_type2 >( input_parameter_type1 argument1, input_parameter_type2 argument2);

input_paramter_type, input_paramter_type1, input_paramter_type2는 입력 매개변수의 유형을 나타내고, 인수1과 인수2는 액션 대리자가 캡슐화한 메소드에 사용되는 매개변수입니다.

C#에서 Action Delegate 작업

  • 반환 값이 없는 메서드, 즉 반환 유형이 void인 메서드에 사용하기 위해 작업 대리자가 필요한 경우 이를 작업 대리자라고 합니다.
  • 액션 대리자는 일반 유형에 속하며 시스템 네임스페이스 내에 정의됩니다.
  • 액션 대리자에 포함될 수 있는 입력 매개변수의 최소 개수는 1개, 액션 대리자에 포함될 수 있는 입력 매개변수의 최대 개수는 16개이며, 사용되는 매개변수 유형은 동일한 데이터일 수 있습니다. 유형 또는 다른 데이터 유형.
  • 프로그램에서 작업 대리자를 사용하면 프로그램이 더욱 최적화되고 가독성이 높아집니다.

아래에 언급된 예는 다음과 같습니다.

예시 #1

주어진 문자열을 연결하고 명령문을 화면 출력으로 인쇄하는 Action Delegate를 시연하는 C# 프로그램.

코드:

using System;
//a class called check is defined
class check
{
// a method called join is called which takes the parameter passed to the method and prints it as the output on the screen
public static void join(string str)
{
Console.WriteLine("Welcome to {0}", str);
}
// main method is called within which the join function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes one input parameter which is passed to the join method
Action<string> stringvalue = join;
stringvalue("C#");
}
}

출력:

C# 작업 대리자

위 프로그램은 "check"라는 클래스를 정의하고 "join"이라는 메서드를 호출합니다. 이 메서드는 전달된 매개 변수를 화면에 출력으로 인쇄합니다. 다음으로, 메인 메소드는 액션 대리자를 정의하여 "join" 함수를 호출합니다. 그런 다음 프로그램은 하나의 입력 매개변수를 사용하는 작업 대리자를 정의합니다.

예시 #2

주어진 숫자의 힘을 계산하는 작업 대리자를 보여주는 C# 프로그램

코드:

using System;
//a class called check is defined
class check
{
// a method called power is defined which takes two parameters passed to the method and calculates the power of the given number and displays it on the screen
public static void power(double number1, double number2)
{
Console.WriteLine("The power of the given number is {0}", Math.Pow(number1, number2));
}
// main method is called within which the power function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes two input parameters which is passed to the power method
Action<double, double> doublevalue = power;
doublevalue(2,2);
}
}

출력:

C# 작업 대리자

제공된 프로그램에서는 "check"라는 클래스를 정의합니다. 이 클래스에는 전달된 두 매개변수의 거듭제곱을 계산하고 그 결과를 화면에 표시하는 "power"라는 메서드가 포함되어 있습니다. 그 후에, 액션 대리자를 정의하여 "power" 함수를 호출하는 메인 메소드를 호출합니다. 또한 두 개의 입력 매개변수를 허용하는 작업 대리자를 정의합니다.

예시 #3

주어진 숫자의 제곱을 찾는 작업 대리자를 시연하는 C# 프로그램

코드:

using System;
//a class called check is defined
class check
{
// a method called power is defined which takes two parameters passed to the method and calculates the power of the given number and displays it on the screen
public static void square(int number)
{
Console.WriteLine("The square of the given number is {0}", number * number);
}
// main method is called within which the power function is called by defining an action delegate
static public void Main()
{
//an action delegate is defined which takes one input parameter which is passed to the square method
Action<int> answer = square;
answer(3);
}
}

출력:

C# 작업 대리자

위 프로그램에서는 "check"라는 클래스를 정의합니다. 이 클래스 내에서 두 개의 매개변수를 입력으로 사용하는 "power"라는 메서드를 정의합니다. 이 방법은 주어진 숫자의 거듭제곱을 계산하고 그 결과를 화면에 표시합니다. 그런 다음 작업 대리자를 정의하여 "power" 함수를 호출하는 기본 메서드를 호출합니다. 구체적으로 두 개의 입력 매개변수를 허용하는 작업 대리자를 정의합니다.

결론

이 튜토리얼에서는 정의를 통해 C#의 액션 대리자의 개념, 액션 대리자의 구문, 프로그래밍 예제와 그 출력을 통해 C#의 액션 대리자의 작동 방식을 이해합니다.

위 내용은 C# 작업 대리자의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C# 읽기 전용다음 기사:C# 읽기 전용