포슀트

πŸŒ’ 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와 λ‹€λ₯Έ 점

  1. delegateλŠ” 클래슀 μ™ΈλΆ€μ—μ„œ 직접 ν˜ΈμΆœν•  수 μžˆμ§€λ§Œ, eventλŠ” 클래슀 λ‚΄λΆ€μ—μ„œλ§Œ ν˜ΈμΆœν•  수 μžˆλ‹€.
  2. delegateλŠ” null을 κ°€μ§ˆ 수 μžˆμ§€λ§Œ, eventλŠ” null을 κ°€μ§ˆ 수 μ—†λ‹€.
  3. 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



이 κΈ°μ‚¬λŠ” μ €μž‘κΆŒμžμ˜ CC BY 4.0 λΌμ΄μ„ΌμŠ€λ₯Ό λ”°λ¦…λ‹ˆλ‹€.