c#中的委託01

delegate 是表示對具備特定參數列表和返回類型的方法的引用的類型。 在實例化委託時,你能夠將其實例與任何具備兼容簽名和返回類型的方法相關聯。 你能夠經過委託實例調用方法。spa

委託用於將方法做爲參數傳遞給其餘方法。 事件處理程序就是經過委託調用的方法。 你能夠建立一個自定義方法,當發生特定事件時,某個類(如 Windows 控件)就能夠調用你的方法(舉個例子,Winform上拖拽完button,而後雙擊,後臺生成這個button的點擊事件,這樣這個button的點擊事件就跟你的方法綁定起來了)。3d

 

需求:遇到到不一樣國家的人,以不一樣方式跟他打招呼。code

 

Talking is easy ,show me the codes .orm

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<People> pp = new List<People>();
            pp.Add(new People { Name = "馬大雲", Country = "中國" });
            pp.Add(new People { Name = "Bill Gat", Country = "USA" });

            pp.ForEach(p => Say(p));
        }

        public static void Say(People p)
        {
            if (p != null && !string.IsNullOrEmpty(p.Country))
            {
                if (p.Country.Equals("中國", StringComparison.OrdinalIgnoreCase))
                {
                    Chinesenihao(p.Name);
                }
                if (p.Country.Equals("USA", StringComparison.OrdinalIgnoreCase))
                {
                    EnglishHello(p.Name);
                }
            }
        }
        public static void Chinesenihao(string name)
        {
            Console.WriteLine($"{name},老表,吃飯沒?");
        }

        public static void EnglishHello(string name)
        {
            Console.WriteLine($"hi,{name},the weather is nice today.");
        }


    }

    public class People
    {
        public string Name { get; set; }
        public string Country { get; set; }
    }
}

上面這種實現是能夠的,知足了需求,當再來幾個國家,Say方法裏面再加幾個就闊以了。blog

可是當你工做幾年後,你還這麼寫,那就不清真(zhuang bi shi bai )了。事件

Talking is easy ,show me the codes .get

 public delegate void sayhellodelegate(string name);
    class Program
    {
       
        static void Main(string[] args)
        {
            
            List<People> pp = new List<People>();
            pp.Add(new People { Name = "馬大雲", Country = "中國" ,sayfunction= Chinesenihao });
            pp.Add(new People { Name = "Bill Gat", Country = "USA" ,sayfunction= EnglishHello });

            pp.ForEach(p => Say(p));
        }

        public static void Say(People p)
        {
            p.sayfunction(p.Name);
        }
        public static void Chinesenihao(string name)
        {
            Console.WriteLine($"{name},老表,吃飯沒?");
        }

        public static void EnglishHello(string name)
        {
            Console.WriteLine($"hi,{name},the weather is nice today.");
        }


    }

    public class People
    {
        public string Name { get; set; }
        public string Country { get; set; }

        public sayhellodelegate sayfunction { get; set; }
    }

上面的代碼中,sayhellodelegate當作一種類型在用。這也是爲何文章開頭的那句是這樣的:delegate 是表示對具備特定參數列表和返回類型的方法的引用的類型。string

 

新需求:遇到到不一樣國家的人,以不一樣方式跟他打招呼,若是有多個國家的國籍,擇使用多種方式打招呼。it

 
static void Main(string[] args) { List<People> pp = new List<People>(); var t = new People { Name = "馬大雲", Country = "中國", sayfunction = Chinesenihao }; t.Country = "中國,USA"; t.sayfunction += EnglishHello; pp.Add(t); pp.Add(new People { Name = "Bill Gat", Country = "USA", sayfunction = EnglishHello }); pp.ForEach(p => Say(p)); }

其餘代碼不動,在給Sayfunction 賦值時坐了追加就知足了需求。io

 

 

這是delegate的另一個特性:

能夠將多個方法賦給同一個委託,或者叫將多個方法綁定到同一個委託,當調用這個委託的時候,將依次調用其所綁定的方法。

 

固然能夠追加,也能夠取消。

 

 

 

使用委託能夠將多個方法綁定到同一個委託變量,當調用此變量時(這裏用「調用」這個詞,是由於此變量表明一個方法),能夠依次調用全部綁定的方法。

相關文章
相關標籤/搜索