C#委託

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   使用   sp   

轉自:http://www.cnblogs.com/ArmyShen/archive/2012/08/31/2664727.html

委託是一個類,它定義了方法的類型,使得可以將方法當作另一個方法的參數來進行傳遞,這種將方法動態地賦給參數的做法,可以避免在程式中大量使用if-else(switch)語句,同時使得程式具有更好的可擴充性。

使用委託可以將多個方法綁定到同一個委託變數上(通常稱這個委託變數為:委託鏈),當調用此變數時,會依次調用所有綁定的方法;於此同時,也可以通過類似綁定的方式刪除方法。

 

一個簡單的委託例子

using System;using System.Collections;namespace Delegate{    //用delegate關鍵字聲明一個委託    //委託原型必須與預委託的方法具有相同的傳回值和參數類型    delegate void LearnDelegate(string name);    public class Test    {
     //要進行委託的方法 static void LearnA(string name) { Console.WriteLine("小王在學" + name); }       static void LearnB(string name) { Console.WriteLine("小張在學" + name); } static void Main() { //建立委託執行個體,並把委託函數名當作參數傳遞給委派物件 LearnDelegate learn_A = new LearnDelegate(LearnA); LearnDelegate learn_B = new LearnDelegate(LearnB); //通過委派物件,給綁定的函數傳遞參數,其實現在的learn_A和learn_B就是learnA和learnB的函數別名 learn_A("C#"); learn_B("C++"); } }}

 

委託鏈

static void Main(){  //建立委託執行個體  LearnDelegate learn_A = new LearnDelegate(LearnA);  LearnDelegate learn_B = new LearnDelegate(LearnB);  //聲明一個委託鏈,不需要執行個體化它  LearnDelegate DelegateLst;  //把委派物件直接相加並賦給委託鏈  DelegateLst = learn_A + learn_B;  //給委託鏈傳值  DelegateLst("編程");  Console.WriteLine();  //同樣,也可以對委託鏈中的委派物件進行添加和刪除操作  DelegateLst -= learnA;//刪除一個委託方法,這裡使用DelegateLst -= Learn_A;效果是一樣的,因為此時可以看作它是LearnA方法的一個別名  DelegateLst("編程");  Console.WriteLine();              DelegateLst += learnB;//添加一個委託方法,同樣這裡也可以使用DelegateLst += Learn_B;  DelegateLst("編程");}

 

委託鏈的簡化使用

using System;using System.Collections;namespace Delegate{    //用delegate關鍵字聲明一個委託    //委託原型必須與預委託的方法具有相同的傳回值和參數類型    delegate void LearnDelegate(string name);    public class Test    {        static void Main()        {            //聲明一個委託鏈,賦null值            LearnDelegate DelegateLst = null;            //下面這種形式其實是匿名方法的一種應用            DelegateLst += delegate(string name) { Console.WriteLine("小王在學" + name); };            DelegateLst += delegate(string name) { Console.WriteLine("小張在學" + name); };            DelegateLst("編程");        }    }}

 

最後的一個例子

using System;using System.Collections;namespace Delegate{    //用delegate關鍵字聲明一個委託    //委託原型必須與預委託的方法具有相同的傳回值和參數類型    delegate void AnimalDelegate(string AnimalName);    class Animal    {        public Animal()        {        }        //要進行委託的方法        public void Miaow(string name)        {            Console.WriteLine( name + " 喵喵叫");        }        //要進行委託的方法        public void Bark(string name)        {            Console.WriteLine(name + " 汪汪叫");        }        //要進行委託的方法        public void Baa(string name)        {            Console.WriteLine(name + " 咩...");        }             }    public class MainFunc    {        static void choseAnimal(string name, AnimalDelegate delegateFun)        {            delegateFun(name);        }        static void Main()        {            /*AnimalDelegate cat = new AnimalDelegate(new Animal().Miaow);            cat("貓咪");            AnimalDelegate dog = new AnimalDelegate(new Animal().Bark);            dog("狗狗");*/                        //上下的兩種調用方式是等價的            choseAnimal("喵星人", new Animal().Miaow);            choseAnimal("汪星人", new Animal().Bark);            choseAnimal("喜洋洋", new Animal().Baa);        }    }}

C#委託

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.