A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure.
다음 예제에서는 string을 인수로 사용하고 void를 반환하는 메서드를 캡슐화할 수 있는 Callback 대리자를 선언합니다.
public delegate void Callback(string message);
1. delegate int CalculateDelegate(int a, int b);
2-1. CalculateDelegate Plus = new CalculateDelegate(Add);
2-2. CalculateDelegate Minus = Subtract
3. DebugLog(1, 2, Plus);
3. DebugLog(4, 3, Minus);
4. void DebugLog(int a, int b, CalculateDelegate calculateDelegate)
{
var res = calculateDelegate.Invoke();
print(res);
}
https://daekyoulibrary.tistory.com/entry/C-콜백-함수Callback는-무엇이고-대리자Delegate는-뭘까
using System;
// Define a custom delegate that has a string parameter and returns void.
delegate void CustomCallback(string s);
class TestClass
{
// Define two methods that have the same signature as CustomCallback.
static void Hello(string s)
{
Console.WriteLine($" Hello, {s}!");
}
static void Goodbye(string s)
{
Console.WriteLine($" Goodbye, {s}!");
}
static void Main()
{
// Declare instances of the custom delegate.
CustomCallback hiDel, byeDel, multiDel, multiMinusHiDel;
// In this example, you can omit the custom delegate if you
// want to and use Action<string> instead.
//Action<string> hiDel, byeDel, multiDel, multiMinusHiDel;
// Initialize the delegate object hiDel that references the
// method Hello.
hiDel = Hello;
// Initialize the delegate object byeDel that references the
// method Goodbye.
byeDel = Goodbye;
// The two delegates, hiDel and byeDel, are combined to
// form multiDel.
multiDel = hiDel + byeDel;
// Remove hiDel from the multicast delegate, leaving byeDel,
// which calls only the method Goodbye.
multiMinusHiDel = multiDel - hiDel
Console.WriteLine("Invoking delegate hiDel:");
hiDel("A");
Console.WriteLine("Invoking delegate byeDel:");
byeDel("B");
Console.WriteLine("Invoking delegate multiDel:");
multiDel("C");
Console.WriteLine("Invoking delegate multiMinusHiDel:");
multiMinusHiDel("D");
}
}
/* Output:
Invoking delegate hiDel:
Hello, A!
Invoking delegate byeDel:
Goodbye, B!
Invoking delegate multiDel:
Hello, C!
Goodbye, C!
Invoking delegate multiMinusHiDel:
Goodbye, D!
*/
공변성(covariance)은 메서드가 대리자에 정의된 것보다 더 많은 수의 파생된 형식을 반환하도록 허용합니다.
class Mammals {}
class Dogs : Mammals {}
class Program
{
// Define the delegate.
public delegate Mammals HandlerMethod();
public static Mammals MammalsHandler()
{
return null;
}
public static Dogs DogsHandler()
{
return null;
}
static void Test()
{
HandlerMethod handlerMammals = MammalsHandler;
// Covariance enables this assignment.
HandlerMethod handlerDogs = DogsHandler;
}
}
반공변성(contravariance)은 메서드가 대리자 형식보다 더 적은 수의 파생된 매개 변수 형식을 갖도록 허용합니다.
// Event handler that accepts a parameter of the EventArgs type.
private void MultiHandler(object sender, System.EventArgs e)
{
label1.Text = System.DateTime.Now.ToString();
}
public Form1()
{
InitializeComponent();
// You can use a method that has an EventArgs parameter,
// although the event expects the KeyEventArgs parameter.
this.button1.KeyDown += this.MultiHandler;
// You can use the same method
// for an event that expects the MouseEventArgs parameter.
this.button1.MouseClick += this.MultiHandler;
}
public delegate void KeyEventHandler(object sender, KeyEventArgs e);
public delegate void MouseEventHandler(object sender, MouseEventArgs e);
using UnityEngine;
using UnityEngine.Events
public class MyEvent : MonoBehaviour {
public UnityEvent myEvent;
private void Awake() {
if (myEvent == null)
myEvent = new UnityEvent();
myEvent.AddListener(SomeFunction);
}
public void TriggerEvent() {
myEvent.Invoke();
}
private void SomeFunction() {
// Do something here
}
}

델리게이트와 유사하지만 이벤트 호출은 본인 클래스만 가능하다.
Action, Func, Predicate, 그리고 Unity의 UnityEvent를 사용하면 코드의 일부를 메서드로 캡슐화하고 쉽게 재사용할 수 있습니다.Action은 반환값이 없는 메서드를 가리키는 델리게이트입니다. 0개 이상의 매개변수를 가질 수 있습니다.csharp코드 복사
using System;
class Program
{
static void Main()
{
Action<int> printNumber = (num) => Console.WriteLine(num);
printNumber(10); // 출력: 10
}
}Func은 반환값이 있는 메서드를 가리키는 델리게이트입니다. 마지막 타입 파라미터가 반환 타입을 나타내며, 최대 16개의 입력 매개변수를 받을 수 있습니다.csharp코드 복사
using System;
class Program
{
static void Main()
{
Func<int, int, int> add = (a, b) => a + b;
int result = add(5, 3); // result = 8
Console.WriteLine(result);
}
}Predicate는 특정 조건을 만족하는지 검사하는 메서드를 가리키는 델리게이트로, 항상 하나의 매개변수를 받고 bool 값을 반환합니다.```csharp
csharp코드 복사
using System;
class Program
{
static void Main()
{
Predicate<int> isEven = (num) => num % 2 == 0;
bool result = isEven(4); // result = true
Console.WriteLine(result);
}
}
```✏️참고 자료
microsoft-using-delegates
델리게이트의 특징 - daekyoulibrary