모름

예제코드

 

class Program {
    delegate void Notify(string message);

    class Notifier { //Notify 대리자의 인스턴스이며 EventOccured를 가지는 클래스입니다.
        public Notify EventOccured;
    }

    class EventListener {
        private string name;
        public EventListener(string name) {
            this.name = name;
        }

        public void SomethingHappend(string message) {
            Console.WriteLine($"{name}.SomethingHappend : {message}");
        }
    }

    static void Main(string[] args) {
        Notifier notifier = new Notifier();
        EventListener listener1 = new EventListener("listener1");
        EventListener listener2 = new EventListener("listener2");
        EventListener listener3 = new EventListener("listener3");

        notifier.EventOccured += listener1.SomethingHappend;
        notifier.EventOccured += listener2.SomethingHappend;
        notifier.EventOccured += listener3.SomethingHappend;
        notifier.EventOccured("You've got mail.");

        Console.WriteLine();

        notifier.EventOccured -= listener2.SomethingHappend;
        notifier.EventOccured("Download complete.");

        Console.WriteLine();

        notifier.EventOccured = new Notify(listener2.SomethingHappend) + new Notify(listener3.SomethingHappend);
        notifier.EventOccured("Nuclear launch detected.");

        Console.WriteLine();

        Notify notify1 = new Notify(listener1.SomethingHappend);
        Notify notify2 = new Notify(listener2.SomethingHappend);

        notifier.EventOccured = (Notify)Delegate.Combine(notify1, notify2);
        notifier.EventOccured("Fire!");

        Console.WriteLine();

        notifier.EventOccured = (Notify)Delegate.Remove(notifier.EventOccured, notify2);
        notifier.EventOccured("RPG!");
    }
}

 

아무래도 += 연산자를 통해 델리게이트를 사용했던 경험때문인지, 다른 방식의 델리게이트 체인은 눈에 잘 익지가 않네요. 실제로 눈에 보기 편하고 쓰기 편한게 +=, -= 연산자이긴 합니다. 체인이라는 이름때문에, 어렵게 엮어있는게 아닐까 했는데 델리게이트에 여러 함수를 담는다는 개념이었습니다.

 

 

 


 

 

 

출력