일반 대리자를 사용하면 대리자 문을 정의할 필요가 없습니다. 시스템 네임스페이스에 정의되어 있습니다.
형식 매개 변수를 사용하여 제네릭 대리자를 정의할 수 있습니다. 예를 들어 -
delegate T myDelegete<T>(T n);
예
다음은 C#에서 일반 대리자를 만드는 방법을 보여주는 예입니다 -
using System;
using System.Collections.Generic;
delegate T myDelegete<T>(T n);
namespace GenericDelegateAppl {
class TestDelegate {
static int num = 5;
public static int AddNum(int p) {
num += p;
return num;
}
public static int MultNum(int q) {
num *= q;
return num;
}
public static int getNum() {
return num;
}
static void Main(string[] args) {
//create delegate instances
NumberChanger nc1 = new NumberChanger(AddNum);
NumberChanger nc2 = new NumberChanger(MultNum);
//calling the methods using the delegate objects
nc1(50);
Console.WriteLine("Value of Num: {0}", getNum());
nc2(10);
Console.WriteLine("Value of Num: {0}", getNum());
Console.ReadKey();
}
}
}