C#委托,委托链,多播委托

委托

概念:委托是一种用户自定义的类型.是一种类.可以把委托看成用来执行方法/函数的一个东西

形式:delegate 函数返回类型 委托名称({可有/可无}参数部分)

实例化:委托类型 实例化名=new 委托类型(方法名称)

代码实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    //父亲委托
    public delegate void delFather(string hand);
    //母亲委托
    public delegate void delMother();
    class Program
    {
        static void Main(string[] args)
        {
            A a = new A();
            B b = new B(a);
            a.Raise("左");
            Console.ReadKey();
        }
    }

    //父母
    class A
    {
        //父亲事件
        public event delFather eventFather;
        //母亲事件
        public event delMother eventMother;

        //方法
        public void fatherCall(string child)
        {
            Console.WriteLine("父亲呼叫孩子", child);
            //调用事件了
            if (eventFather != null)
            {
                eventFather(child);
            }
        }

        //方法
        public void motherCall()
        {
            Console.WriteLine("母亲呼叫孩子");
            //调用事件了
            if (eventMother != null)
            {
                eventMother();
            }
        }
    }

    //孩子B
    class B
    {
        A a;
        public B(A a)
        {
            this.a = a;
            a.eventFather += new delFather(a_Father);
            a.eventMother += new delMother(a_Mother);
        }
        void a_Father(string hand)
        {
            if (hand.Equals("左"))
            {
                Dao();
            }
        }
        void a_Mother()
        {
            Dao();
        }
        public void Dao()
        {
            Console.WriteLine("我来了");
        }
    }
}

多播委托

  多播委托即对同一个委托进行多次+=订阅,但每次执行的结果都会被下次的执行结果所覆盖,最后得到的结果即订阅最后一个方法的值

*多播委托是先创建一个委托实例,在这个实例上进行订阅*

代码实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 多播委托
{


    class Program
    {
        public delegate int Add(out int x);
        static void Main(string[] args)
        {
            Add a = new Add(One);
            a += new Add(Two);
            int x;
            int y = a(out x);
            Console.WriteLine(y);
            Console.ReadKey();
        }
        public static int One(out int x)
        {
            return x = 1;
        }
        public static int Two(out int x)
        {
            return x = 2;
        }
    }
}

委托链

  委托链形式类似于多播委托

*委托链是创建多个委托实例,每个实例与每个实例之间进行订阅*

namespace 委托链
{
    class Program
    {
        public delegate void Chain();
        static void Main(string[] args)
        {
            Chain c = new Chain(Method1);
            Chain c1 = new Chain(Method2);
            Chain c2 = null;

            //委托链中 添加委托
            //使用+=  
            c2 += c;
            c2 += c1;

            //委托链中 移除委托
            c2 -= c;

            c2();
            Console.ReadKey();
        }
        static void Method1()
        {
            Console.WriteLine("method1");
        }
        static void Method2()
        {
            Console.WriteLine("method2");
        }
    }
}