[C# 객체지향] 타입 유형 확장2_델리게이트

eunjin lee·2022년 6월 25일
0

C# 9.0 프로그래밍

목록 보기
10/50

C#에서는 메서드를 값으로 갖는 타입을 delegate 구문으로 제공한다.



1. 방법

  • int Clean (object arg);
    → delegate int FunDelegate(object arg);
    = 예약어 반환값 타입명 (인자)

  • ✍ 샘플 코드

    internal class Program
    {
        delegate void DelegateCry();
        delegate void DelegateSleep();
        static void Main(string[] args)
        {
            Animal a = new Animal();
            DelegateCry dc = new DelegateCry(a.Cry);
            DelegateSleep ds = a.Sleep;
     
        }
    }


2. 실체

  • delegate는 타입이다.
  • 클래스 내부에서 delegate를 정의하는 것은 중첩 클래스일 뿐이다.
  • System.MulticastDelegate 타입은 System.Delegate 타입을 상속받고, 이것은 다시 System.Object를 상속받는다.


3. 메서드의 추가나 제거

  • 한번의 함수 호출로 여러 메서드가 호출될 수 있고, 상황에 따라 호출되는 메서드를 더하거나 뺄 수 있다.
  • ✍ 샘플 코드
    internal class Program
    {
        delegate void DelegateCalculate(int x, int y);

        static void Main(string[] args)
        {
            DelegateCalculate dc = Calculator.Add;
            dc += Calculator.Substract;
            dc += Calculator.Divide;
            dc += Calculator.Multiply;

            dc(10, 5);

            dc -= Calculator.Divide;
            dc(5, 0);
        }
    }

    class Calculator
    {
        static public void Add(int x, int y) { Console.WriteLine(x + y); }
        static public void Substract(int x, int y) { Console.WriteLine(x - y); }
        static public void Divide(int x, int y) { Console.WriteLine(x / y); }
        static public void Multiply(int x, int y) { Console.WriteLine(x * y); }
    }
  • ✅ 결과
    15
    5
    2
    50

    5
    5
    0
  1. 콜백 메서드
  • 피호출자에서 호출자의 메서드를 호출하는 것.
  • 꼭 호출자의 메서드가 아니더라도, 피호출자가 호출자로부터 다른 타입에 정의된 메서드를 전달받아 호출하는 것.
  • ✍ 샘플 코드
namespace Pjt
{
    delegate void DelegateMethod();
    internal class Caller
    {
        static void Main(string[] args)
        {
            DelegateMethod dm = CallBackMethod;
            CalledClass c = new CalledClass();
            c.Work(dm);
        }

        static void CallBackMethod()
        {
            Console.WriteLine("Called Backed");
        }
    }

    class CalledClass
    {
        public void Work(DelegateMethod dm)
        {
            Console.WriteLine("Called Class Do Its Business...");
            dm();
        }
    }

}
  • ✅ 결과
  Called Class Do Its Business...
  Called Backed
  • ✍ 샘플 코드
namespace Pjt
{
    delegate void DelegateMethod();
    internal class Caller
    {
        static void Main(string[] args)
        {
            DelegateMethod dm = CallBackClass.CallBackMethod;
            CalledClass c = new CalledClass();
            c.Work(dm);
        }

        static void CallBackMethod()
        {
            Console.WriteLine("Called Backed");
        }
    }

    class CalledClass
    {
        public void Work(DelegateMethod dm)
        {
            Console.WriteLine("Called Class Do Its Business...");
            dm();

        }
    }

    class CallBackClass
    {
        public static void CallBackMethod()
        {
            Console.WriteLine("Called Back in Another Class");
        }
    }

}
  • ✅ 결과
  Called Class Do Its Business...
  Called Back in Another Class

0개의 댓글