天天看點

c#委托

示例1:

c#委托

代碼

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

    delegate void EatDelegate(String food);

    class Program

    {

        private static void zsEat(String food)

        {

            Console.WriteLine("張三吃"+food);

        }

        private static void lsEat(String food)

            Console.WriteLine("李四吃"+food);

        private static void wwEat(String food)

            Console.WriteLine("王五吃"+food);

        static void Main(string[] args)

            /*定義張三,李四,王五吃food的委托,

             * 并為他們的委托代理他們吃food的方法.*/

            EatDelegate zs = new EatDelegate(zsEat);

            EatDelegate ls = new EatDelegate(lsEat);

            EatDelegate ww = new EatDelegate(wwEat);

            //委托鍊

            EatDelegate eatChain = zs + ls + ww;

            Console.WriteLine("張三,李四,王五開座談會.");

            //調用委托鍊

            eatChain("吃西瓜");

            Console.WriteLine("李四出去接電話.");

            /*C#在委托中重載了+=和-=操作符,

             * 使它們能夠對委托鍊進行+,-操作*/

            eatChain -= ls;

            eatChain("吃香蕉");

            Console.WriteLine("李四回來了.");

            eatChain += ls;

            eatChain("吃桔子.");

    }

}

 委托的匿名方法:

c#委托

            EatDelegate eatChain=null;

            //C#2.0增加的委托的匿名方法

            eatChain += delegate(String food) { Console.WriteLine("張三吃"+food); };

            eatChain += delegate(String food) { Console.WriteLine("李四吃"+food); };

            eatChain += delegate(String food) { Console.WriteLine("王五吃" + food); };

            eatChain("西瓜");

 委托代理執行個體方法

c#委托
c#委托

    class Man

        private string name;

        public Man(string name)

            this.name = name;

        public void Eat(string food)

            Console.WriteLine(name+"吃"+food);

    /*委托代理執行個體方法*/

    class Party

        [STAThread]

            Man zsObj = new Man("張三");

            Man lsObj = new Man("李四");

            Man wwObj = new Man("王五");

            EatDelegate zs = new EatDelegate(zsObj.Eat);

            EatDelegate ls = new EatDelegate(lsObj.Eat);

            EatDelegate ww = new EatDelegate(wwObj.Eat);

            EatDelegate eatChain=zs+ls+ww;

            eatChain("吃桔子");

 委托當作方法參數進行調用:

c#委托
c#委托

代碼

    /*委托作為方法的參數進行傳遞*/

        /// <summary>

        /// 委托作為參數進行傳遞

        /// </summary>

        /// <param name="food">吃的東西</param>

        /// <param name="values">可變的委托參數</param>

        static void EatToghter(String food, params EatDelegate[] values)

            if (values==null)

            {

                Console.WriteLine("座談會結束");

            }

            else

                EatDelegate eatChain = null;

                foreach (EatDelegate eat in values)

                    eatChain += eat;

                eatChain(food);

                Console.WriteLine();

            //調用委托作為參數的方法

            EatToghter("西瓜",zs,ls,ww);

            //沒有傳遞李四的委托

            EatToghter("香蕉", zs, ww);

            //三個人繼續吃香蕉

            EatToghter("香蕉", zs, ls, ww);

            EatToghter(null,null);