본문 바로가기
c#/수업 내용

멀티캐스트 대리자 정의

by 이지훈26 2021. 9. 7.

(+) 연산자를 사용하여 하나의 대리자 인스턴스에 여러 개체를 할당할 수 있다

멀티캐스트 대리자는 할당된 대리자 목록을 포함합니다.

멀티캐스트 대리자가 호출되면 목록에 있는 대리자가 순서대로 호출됩니다.

같은 형식의 대리자만 결합할 수 있습니다.

( - ) 연산자는 멀티캐스트 대리자에서 구성 요소 대리자를 제거하는 데 사용할 수 있다

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld1
{
    class App
    {
        //1.대리자 선언
        delegate void CustomDel(string s);

        //2.대리자 변수 선언
        CustomDel hiDel;
        CustomDel byeDel;
        CustomDel multiDel;
        CustomDel multiMinusHiDel;


        //생성자
        public App()
        {
            //4.대리자 인스턴스화
            this.hiDel = this.Hello;
            this.byeDel = this.Goodbye;

            //5.대리자 더하기
            this.multiDel = this.hiDel + this.byeDel;

            //6.대리자 호출
            //this.multiDel("hong");

            //멀티캐스트 대리자로 부터 대리자 제거하기
            //this.multiMinusHiDel = this.multiDel - this.hiDel;
            //this.multiDel -= this.hiDel;

            //대리자 호출
            //this.multiMinusHiDel("lim");
        }

        //3.대리자에 연결할 메서드 정의
        void Hello(string s)
        {
            Console.WriteLine("Hello: {0}", s);
        }

        void Goodbye(string s)
        {
            Console.WriteLine("Goodbye: {0}", s);
        }
    }
}

순서대로 진행 후, 멀티캐스트 대리자로 부터 대리자 제거 -> 대리자 호출

'c# > 수업 내용' 카테고리의 다른 글

람다 식 연습  (0) 2021.09.07
대리자 익명 함수 정의 (*꼭 알아야 할 것*)  (0) 2021.09.07
오전 수업 대리자 연습 (고블린, SCV)  (0) 2021.09.07
대리자 수정하기 연습  (0) 2021.09.06
대리자 (DalyGate) 정의  (0) 2021.09.06