: 다른 함수를 참조하여 실행하는, 즉 메소드를 참조하는 변수
[콜백으로 사용: 하나의 메소드에 다양한 delegate를 파라미터로 넣을수있음]
: 이벤트, 스레드동기화, Reflect등에 많이 쓰인다.
: delegate를 선언해준다. delegate return_type delegate_name (params)
// 반환타입과 파라미터 타입은 사용코자 하는 메소드와 동일해야 한다.
: delegate 타입에 변수를 생성하고, 생성된 변수에 해당 메소드를 참조시킨다.
: 사용시에는 생성된 변수에 파라미터를 넣는다.
- Delegate 실행
using System;
namespace Example
{
//Delegate 선언
delegate void Ex_Dele(int num);
public class Program
{
// 대리자 함수
public static void Ex1_Method(int num)
{
Console.WriteLine(num);
}
// 대리자 함수
public static void Ex2_Method(int num)
{
num += 100;
Console.WriteLine(num);
}
// execute
public static void Main(string[] args)
{
// Delegate 변수 선언 및 참조 인스턴스에 대리자함수 입력
Ex_Dele ed1 = new Ex_Dele(Ex1_Method);
// Delegate 실행
ed1(100);
Ex_Dele ed2 = new Ex_Dele(Ex2_Method);
ed2(100);
}
}
}
- Delegate 변수를 파라미터로 사용
: Delegate와 대리함수를 선언하고, 이 메소드를 값으로 하는 인스턴스를 참조하는 Delegate 변수를 다른 함수의 파라
미터로 사용
using System;
namespace Example
{
delegate int Ex_Dele(int num);
public class Program
{
public static int Ex_Method(int num)
{
return num *= 100;
}
public static void Ex2_Method(int exd)
{
Console.WriteLine(exd);
}
// execute
public static void Main(string[] args)
{
// Delegate 인스턴스에 메소드를 파라미터로 넣어 ed 변수생성
Ex_Dele ed = new Ex_Dele(Ex_Method);
// 참조변수를 다른 함수의 파라미터로 사용
Ex2_Method(ed(1));
Ex2_Method(ed(10));
}
}
}
/* 100 1000 */
- Delegate 콜백함수로 실행
using System;
namespace Example
{
public delegate int Ex_Dele(int num);
public class Program
{
public static int Ex_Method(int num)
{
return num *= 100;
}
// Delegate를 파라미터로 사용
public static void Ex2_Method(int n, Ex_Dele exd)
{
// 파라미터로 받은 값을 delegate 인스턴스의 값으로 입력)
Console.WriteLine(exd(n));
}
// execute
public static void Main(string[] args)
{
Ex_Dele ed = new Ex_Dele(Ex_Method);
Ex2_Method(10, ed);
}
}
}
/* 1000 */
- Delegate 콜백함수로 사용 응용
using System;
namespace Example
{
// Delegate 선언
public delegate int CalDelegate(int x, int y);
public class Program
{
// Delegate를 파라미터로 받는 함수
public static void Calculator(int x, int y, string type, CalDelegate cd)
{
Console.WriteLine(x + " " + type + " " + y + " is " + cd(x, y));
}
// 대리함수
public static int add(int x, int y)
{
return x + y;
}
// 대리함수
public static int substract(int x, int y)
{
return x - y;
}
// 대리함수
public static int multiple(int x, int y)
{
return x * y;
}
// 대리함수
public static int divide(int x, int y)
{
return x / y;
}
// execute
public static void Main(string[] args)
{
// Delegate 변수 선언 및 인스턴스에 대리함수 입력
CalDelegate Add = new CalDelegate(add);
CalDelegate Substract = new CalDelegate(substract);
CalDelegate Multiple = new CalDelegate(multiple);
CalDelegate Divide = new CalDelegate(divide);
// Delegate 변수값을 파라미터로 받는 함수실행
Calculator(10, 20, "add", Add);
Calculator(30, 10, "substrat", Substract);
Calculator(10, 20, "multiple", Multiple);
Calculator(50, 10, "divide", Divide);
}
}
}
/*
10 add 20 is 30
30 substrat 10 is 20
10 multiple 20 is 200
50 divide 10 is 5
*/
Action, Func
: delegate를 좀더 간편하게 쓰기 위한 메소드
: Action은 리턴값이 없고, Func는 리턴값을 갖는다(마지막 파라미터가 리턴값)
using System;
namespace Example
{
//Delegate 선언
// delegate void Ex_Dele(int num);
public class Program
{
// 대리자 함수
//public static void Ex1_Method(int num)
//{
// Console.WriteLine(num);
//}
// 대리자 함수
//public static void Ex2_Method(int num)
//{
// num += 100;
// Console.WriteLine(num);
//}
// execute
public static void Main(string[] args)
{
// Action - 리턴값이 없음
Action<int> action = delegate(int num)
{
Console.WriteLine(num);
};
// Func - 리턴값이 int
Func<int, int> func = delegate (int num)
{
num += 100;
return num;
};
// Delegate 실행
action(100);
Console.WriteLine(func(200));
}
}
}
/* 100 300 */
Lambda
: Action, Func를 더 간단히 표현
using System;
namespace Example
{
public class Program
{
static Action<int> action;
static Func<int, int> func;
// execute
public static void Main(string[] args)
{
action = (int num) =>
{
Console.WriteLine(num);
};
// Delegate 변수 선언 및 참조 인스턴스에 대리자함수 입력
func = (int num) =>
{
num += 100;
return num;
};
// Delegate 실행
action(100);
//Ex_Dele ed2 = new Ex_Dele(Ex2_Method);
Console.WriteLine(func(200));
}
}
}
/* 100 300 */
'ASP.NET C#' 카테고리의 다른 글
Generic(제너릭) (0) | 2021.01.27 |
---|---|
yield, paraller, dynamic (0) | 2021.01.21 |
Members => Property(프로퍼티) (0) | 2021.01.16 |
enum(열거형) (0) | 2021.01.15 |
상속, abstract, interface, sealed, this, base (0) | 2021.01.12 |