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的另一个特性:

能够将多个方法赋给同一个委托,或者叫将多个方法绑定到同一个委托,当调用这个委托的时候,将依次调用其所绑定的方法。

 

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

 

 

 

使用委托能够将多个方法绑定到同一个委托变量,当调用此变量时(这里用“调用”这个词,是由于此变量表明一个方法),能够依次调用全部绑定的方法。

相关文章
相关标签/搜索