C# 委托(Delegate)

C# 中的委托(Delegate)类似于 C 或 C++ 中的函数指针。委托(Delegate) 是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。

委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 System.Delegate 类。

  1. 声明一个委托(函数指针)
  2. 创建委托对象
  3. 创造符合委托格式的函数。(指针指向的函数)
  4. 将函数名称赋值给委托

声明委托(Delegate)
声明委托的语法如下:
delegate <return type> <delegate-name> <parameter list>
例如:
public delegate void Callback ();

class Program
    {
        public delegate void Callback();
        public Callback m_cb;
        public void RealFunction()
        {
            Console.WriteLine("this is delegate callback");
        }

        public void RealFunction2()
        {
            Console.WriteLine("this is delegate callback2");
        }

        public void test()
        {
            Callback cb = RealFunction;
            cb();
        }

        public void test2(Callback cb)
        {
            Delegate d = cb;
            Callback r = d as Callback;
            r();
        }
        static void Main(string[] args)
        {
            Program m = new Program();
            m.test();
            m.test2(m.test);

            m.m_cb += new Callback(m.RealFunction);
            m.m_cb += m.RealFunction2;

            m.m_cb -= m.RealFunction;
            m.m_cb();
            Console.ReadLine();
        }
    }

委托的实例化
https://blog.csdn.net/sam1111/article/details/9773

内置的委托

public class DelegationDemo
    {
        public void ActionNoParameter()
        {
            Console.WriteLine("实例化一个Action委托");
        }

        private void ShowResult(int a, int b)
        {
            Console.WriteLine(a + b);
        }

        public bool Match(int val)
        {
            return val > 60;
        }

        private int Add(int a, int b)
        {
            return a+b;
        }

        public void Main()
        {
            {
                Action t = ActionNoParameter;
                t();

                //用Lambd表达式直接把方法定义在委托中
                t = () =>
                {
                    Console.WriteLine("实例化一个Action委托");
                };
                t();

                Delegate d = t;
                var vd = d as Action;
                vd();
            }

            {
                //public delegate void Action<in T1, in T2>(T1 arg1, T2 arg2); 声明原型
                Action<int, int> t = new Action<int, int>(ShowResult);//两个参数但没返回值的委托
                t(2, 3);
                t = (a, b) => { Console.WriteLine(a + b); };
                t(2, 3);

                Delegate d = t;
                var vd = d as Action<int, int>;
                vd(2, 3);
            }

            {
                Func<int, int, int> t = Add;
                Console.WriteLine(t(2,3));
            }

            {
                int[] arr = { 13, 45, 26, 98, 3, 56, 72, 24 };

                Predicate<int> t = Match;   //定义一个比较委托
                int first = Array.Find(arr, t);                 //找到数组中大于60的第一个元素
                Console.WriteLine(first);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            DelegationDemo demo = new DelegationDemo();
            demo.Main();
            Console.ReadLine();
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。