π C# Delegate Event Action Func
π« Delegate
Like ν¨μ ν¬μΈν° in C++.
μ£Όλ‘ μ΄λ²€νΈ μ²λ¦¬, λΉλκΈ° μ²λ¦¬, μ½λ°± ν¨μλ₯Ό ꡬνν λ μ¬μ©λλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
delegate void MyDelegate(string msg);
class MyClass
{
public void Method1(string msg)
{
Console.WriteLine(msg);
}
}
class Program
{
static void Main()
{
MyClass myClass = new MyClass();
MyDelegate myDelegate = new MyDelegate(myClass.Method1);
myDelegate("Hello, World!");
}
}
ν¨μ 체μΈμ λ§λ€ μ μλ€.
λ©ν°μΊμ€νΈ -> μ¬λ¬ λ©μλλ₯Ό λμμ νΈμΆν μ μλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
delegate void MyDelegate(string msg);
class MyClass
{
public void Method1(string msg)
{
Console.WriteLine(msg);
}
public void Method2(string msg)
{
Console.WriteLine(msg);
}
}
class Program
{
static void Main()
{
MyClass myClass = new MyClass();
MyDelegate myDelegate = new MyDelegate(myClass.Method1);
myDelegate += new MyDelegate(myClass.Method2);
myDelegate("Hello, World!");
}
}
무λͺ ν¨μμ λλ€μμ μ¬μ©ν μ μλ€.
1
2
3
4
5
6
7
8
9
10
11
delegate void MyDelegate(string msg);
class Program
{
static void Main()
{
MyDelegate myDelegate = delegate(string msg) { Console.WriteLine(msg); };
myDelegate += (string msg) => { Console.WriteLine(msg); };
myDelegate("Hello, World!");
}
}
λΉλκΈ° μ²λ¦¬ ν μ μκΈ΄ νλ°,
async
await
Task
λ₯Ό μ°λ κ²μ΄ λ λ°λμ§νλ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// https://www.csharpstudy.com/Threads/async-delegate.aspx
class Program
{
static void Main(string[] args)
{
Func<int, int, int> work = GetArea;
// λΈλ¦¬κ²μ΄νΈκ°μ²΄λ‘λΆν° BeginInvokeλ₯Ό μ€ν
// BeginInvokeλ λΉλκΈ°λ‘ μ°λ λλ₯Ό μ€ννλ€.
// μ°λ λκ° μ€νλλ©΄ IAsyncResultλ₯Ό 리ν΄νλ€.
IAsyncResult asyncRes = work.BeginInvoke(10, 20, null, null);
Console.WriteLine("Do something in Main thread");
// λΈλ¦¬κ²μ΄νΈκ°μ²΄λ‘λΆν° EndInvoke(IAsyncResult) μ€ν
// EndInvokeλ μ°λ λκ° μλ£λκΈΈ κΈ°λ€λ¦°λ€.
// μλ£ν λ¦¬ν΄ κ°μ λλ €λ°λλ€.
int result = work.EndInvoke(asyncRes);
Console.WriteLine("Result: {0}", result);
}
static int GetArea(int height, int width)
{
int area = height * width;
return area;
}
}
π« Event
delegate
λ₯Ό μ΄μ©νμ¬ μ΄λ²€νΈλ₯Ό ꡬνν μ μλ€.
event
ν€μλλ₯Ό μ¬μ©νμ¬ μ΄λ²€νΈλ₯Ό μ μΈνλ€.
μ΄λ²€νΈλ ν΄λμ€ μΈλΆμμ λ°μνλ νΉμ ν λμμ μ²λ¦¬ν μ μκ² ν΄μ€λ€.
delegate
μ λ€λ₯Έ μ
delegate
λ ν΄λμ€ μΈλΆμμ μ§μ νΈμΆν μ μμ§λ§,event
λ ν΄λμ€ λ΄λΆμμλ§ νΈμΆν μ μλ€.delegate
λnull
μ κ°μ§ μ μμ§λ§,event
λnull
μ κ°μ§ μ μλ€.delegate
λinterface
λ΄λΆμμ μ¬μ©ν μ μμ§λ§,event
λinterface
λ΄λΆμμ μ¬μ©ν μ μλ€.
μμ μ±μ μν΄ λͺ κ°μ§ μ νμ κ°μ§ delegate
λΌκ³ μκ°νλ©΄ λλ€.
κ°μ²΄μ μν λ³ν, μ¬κ±΄μ λ°μμ μ리기 μν΄ μ¬μ©λλ€.
μ΄λ₯Ό ν΅ν΄ Observer
ν¨ν΄μ ꡬνν μ μλ€.
Observer
ν¨ν΄μ κ°μ²΄μ μν λ³νλ₯Ό λ€λ₯Έ κ°μ²΄μκ² μ리λ ν¨ν΄μ΄λ€.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
delegate void MyDelegate(string msg);
// Publisher
class MyClass
{
public event MyDelegate MyEvent;
// ꡬλ
public void Subscribe(MyDelegate myDelegate)
{
MyEvent += myDelegate;
}
// ꡬλ
ν΄μ§
public void Unsubscribe(MyDelegate myDelegate)
{
MyEvent -= myDelegate;
}
// μ΄λ²€νΈλ₯Ό λ°μμν€λ λ©μλ
public void OnEvent(string msg)
{
MyEvent?.Invoke(msg);
}
}
// Subscriber
class Program
{
static void Main()
{
MyClass myClass = new MyClass();
myClass.Subscribe(MyMethod);
// Cannot Call Directly
// myClass.MyEvent?.Invoke("Hello, World!");
myClass.OnEvent("Hello, World!");
}
static void MyMethod(string msg)
{
Console.WriteLine(msg);
}
}
π« Action
μΌμΌμ΄ delegate
λ₯Ό μ μνλ κ²μ΄ λ²κ±°λ‘μΈ λ,
λ°ν κ°μ΄ μλ λ©μλλ₯Ό μ°Έμ‘°ν μ μλ delegate
.
1
2
3
4
5
Action<string> myAction = (msg) => Console.WriteLine(msg);
myAction("Hello, World!");
Action<int, int> myAction = (a, b) => Console.WriteLine(a + b);
myAction(1, 2);
π« Func
μΌμΌμ΄ delegate
λ₯Ό μ μνλ κ²μ΄ λ²κ±°λ‘μΈ λ,
λ°ν κ°μ΄ μλ λ©μλλ₯Ό μ°Έμ‘°ν μ μλ delegate
.
1
2
Func<int, int, int> myFunc = (a, b) => a + b;
Console.WriteLine(myFunc(1, 2)); // 3