Пример #1
0
        delegate void sayDelegate(); // 대리자 생성

        public static void output()
        {
            WriteLine("40. 대리자");
            WriteLine("대리자란, 함수 자체를 데이터 하나로 보고 대신 실행시켜 주는 기능이다.");
            WriteLine("delegate 키워드를 이용하여 생성하며, 한번에 메서드 하나 이상을 대신해서 호출할 수 있는 개념이다.");

            sayDelegate say = Hi;

            Write("일반 선언 >> ");
            say();

            Write("인스턴스 선언 >>");
            var hi = new sayDelegate(Hi);

            hi();

            WriteLine("또한, 대리자는 += 를 이용하여 대신 선언할 메서드를 하나 이상 등록할 수 있다.");

            hi += new sayDelegate(GoodBye);
            WriteLine("GoodBye 함수 추가 >> ");
            hi();
            WriteLine("\n또한, -=를 이용해 메서드를 제외할 수도 있다.\n");
            hi -= new sayDelegate(Hi);
            hi();
            WriteLine("\n메서드는 더해진 순서대로 실행된다.\n");
            hi += new sayDelegate(Hi);
            hi();
        }
Пример #2
0
        static void Main()
        {
            sayDelegate        sd = name => "hello" + name + "have a nice day.";
            calculatorDelegate cd = (a, b, c) => Console.WriteLine(a * b * c);

            Console.WriteLine(sd("raju"));
            Console.WriteLine(sd("naresh"));
            Console.WriteLine(sd("nacre"));
            cd(10, 20, 30); cd(40, 50, 60); cd(70, 80, 80);
            Console.ReadLine();
        }
Пример #3
0
        static void Main(string[] args)
        {
            Program p = new Program();             // Instantiated to class program

            //p.Addnums(100, 50); //output =150;

            AddDelegate Ad = new AddDelegate(p.Addnums);            //Passing the mmethod addnums to the delegates
            sayDelegate Sd = new sayDelegate(sayHello);             //passing the method SayHello to Delegate

            Ad(1, 2);
            Sd(" Folks ");

            Console.ReadKey();
        }
Пример #4
0
        static void Main(string[] args)
        {
            showLove(chineseSay, "贝贝");
            showLove(englishSay, "JingJing");

            sayDelegate say1;                                         // 委托变量的声明和实例化

            say1 = chineseSay;                                        // say1 是 chineseSay 的委托
            sayDelegate say2 = new sayDelegate(englishSay);           // say2 是 englishSay 的委托

            say1("欢欢");                                               // 两种输出方法
            showLove(say2, "YingYing");

            sayDelegate say3;                                           // 委托的组合(多播):同时将 chineseSay 和 englishSay 注册给 say3

            say3  = chineseSay;
            say3 += englishSay;                                         // 将 englishSay 注册到委托变量 say3 上
            say3("妮妮");

            say3 -= chineseSay;                                         // 将 chineseSay 在委托变量 say3 上注销
            say3("妮妮");

            Console.ReadLine();
        }
Пример #5
0
        /* 原 showLove 函数:
         * public static void showLove(int language, string name)
         * {
         *     if (language == 1) chineseSay(name);
         *     else englishSay(name);
         * }
         *
         * 原主函数:
         * static void Main(string[] args)
         * {
         *     showLove(1, "贝贝");
         *     showLove(2, "JingJing");
         *     Console.ReadLine();
         * }                               */

        public static void showLove(sayDelegate say, string name)       // 使用委托重新定义函数,在新函数中使用函数作为新函数的参数。
        {
            say(name);
        }