본문 바로가기

C#

[C#] delegate

 

delegate

 

대리자라는 의미, 메소드에 대한 참조를 갖는 형식

 

    public class DelegateTest
    {
        // 대리자 생성
        // 매개변수가 없고 반환타입이 void인 메소드만 참조 가능
        delegate void Delegate();

        /*
        static void Main()
        {
            // 대리자 객체 생성
            Delegate myDelegate;
            // Print 메소드 참조
            myDelegate = Print;
            // 대리자를 이용한 메소드 호출
            myDelegate();
            // 메소드를 참조하는 형식
            // 1.
            myDelegate = new Delegate(Print);
            // 2.
            myDelegate = Print;
            // 3.
            myDelegate += Print;
        }
        */

        public static void Print()
        {
            Console.WriteLine("대리자를 통한 메소드 호출");
        }
    }

 

 

Func

 

Func 대리자는 0~16개의 매개변수가 존재하며, 반환값이 존재한다.

.Net FrameWork에 다음과 같이 선언되어있다.

 

public delegate returnType Func<in inputType, out returnType>(InputType arg);

returnType 반환타입

in in 뒤에는 매개변수의 자료형을 정의

out out 뒤에는 메소드의 반환타입을 정의

InputType 메소드의 매개변수

 

 

Action

 

Action 대리자는 0~16개의 입력 매개변수를 가지며, 반환값이 없는 로직을 처리할 때 사용한다.

반환값이 없다는 점을 제외하고는 Func 대리자와 동일하다.

 

    public class FuncAndActionTest
    {

        // Action<T>
        // 반환값이 필요없는 Event
        public event Action<int, bool> ActionEvent = null;

        // Func<T>
        // 반환값이 필요한 Event
        public event Func<int, bool, bool> FuncEvent = null;
        
        public void SendActionEvent()
        {
            if (ActionEvent != null)
            {
                ActionEvent(14, false);
            }
        }

        public void SendFuncEvent()
        {
            if (null != FuncEvent)
            {
                if (FuncEvent(14, false))
                {
                    Console.WriteLine("FuncEvent 결과 : true");
                }
                else
                {
                    Console.WriteLine("FuncEvent 결과 : false");
                }
            }
        }

        public class FuncAndActionConsumer
        {
            public FuncAndActionTest funcAndActionTest;

            public FuncAndActionConsumer()
            {
                funcAndActionTest = new FuncAndActionTest();
                funcAndActionTest.ActionEvent += FuncAndActionTest_ActionEvent;
                funcAndActionTest.FuncEvent += FuncAndActionTest_FuncEvent;
            }

            private bool FuncAndActionTest_FuncEvent(int arg1, bool arg2)
            {
                // 이벤트 처리
                // 100까지의 랜덤수를 2로 나누어 떨어지면 true, 아니면 false
                bool result = arg1 % 2 == 0 ? true : false;

                // 결과 반환
                return result;
            }

            private void FuncAndActionTest_ActionEvent(int arg1, bool arg2)
            {
                // 이벤트 처리
                // 반환값 없음
            }
        }
    }

 

'C#' 카테고리의 다른 글

[C#] DataSet, DataTable, DataRow  (0) 2022.12.11
[C#] ?. ?? 연산자  (0) 2022.12.11
[C#] Parse, TryParse, Convert  (0) 2022.10.10
[C#] 제네릭 generic  (0) 2022.10.10
[C#] 확장 메소드 Extension Method  (0) 2022.10.07