(C#) 34. Event (이벤트)
Syntax (문법)
(C#) 34. Event (이벤트)
Event () : Syntax ()
- 최초 작성일: 2021년 3월 21일(월)
##
InputManager.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Event
{
// Observer Pattern
class InputManager
{
public delegate void OnInputKey(); // 함수 자체를 인자로 넘길 때 좋다.
public event OnInputKey InputKey;
public void Update()
{
if (Console.KeyAvailable == false) // 아무 키도 입력 안함
return;
ConsoleKeyInfo info = Console.ReadKey();
if (info.Key == ConsoleKey.A)
{
// 모두한테 알려준다!
InputKey();
}
}
}
}
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
namespace Event
{
class Program
{
static void OnInputTest()
{
Console.WriteLine("Input Received!");
}
static void Main(string[] args)
{
InputManager inputManager = new InputManager();
inputManager.InputKey += OnInputTest;
while (true)
{
inputManager.Update();
}
}
}
}
Result
‘A’ ‘a’ “Input Received!” .
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.
