포스트

(C#) 33. Delegate (대리자)

Syntax (문법)

(C#) 33. Delegate (대리자)

Delegate () : Syntax ()

  • 최초 작성일: 2021년 3월 21일(월)

##

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
46
47
48
using System;

namespace Delegate
{
    class Program
    {
        // 업체 사장 - 사장님의 비서
        // 우리의 연락처/용건
        // 거꾸로 -> 연락을 달라고

        delegate int OnClicked();
        // delegate -> 형식은 형식인데, 함수 자체를 인자로 넘어주는 그런 형식
        // 반환: int,  입력: void
        // Onclicked이 delegate 형식의 이름이다.

        // UI
        static void ButtonPressed(OnClicked clickedFunction/* 함수 자체를 인자로 넘겨주고 */)
        {
            // 함수를 호출();
            clickedFunction();
        }

        // [ 10 20 40 30 50 ]

        static int TestDelegate()
        {
            Console.WriteLine("Hello Delegate");
            return 0;
        }
        static int TestDelegate2()
        {
            Console.WriteLine("Hello Delegate 2");
            return 0;
        }

        static void Main(string[] args)
        {
            // delegate (대리자)
            Console.WriteLine();

            OnClicked clicked = new OnClicked(TestDelegate);
            clicked += TestDelegate2;

            ButtonPressed(clicked);
        }
    }
}


Result

image

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.