美文网首页
C# 委托(Delegate)

C# 委托(Delegate)

作者: jojo911 | 来源:发表于2018-10-20 15:11 被阅读0次

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();
        }
    }

相关文章

  • C# 委托

    C#委托 C#中的委托(Delegate)类似于C或C++中函数的指针。委托(Delegate)是存有对某个方法的...

  • C# 高级语言总结

    后续 1 C# 委托 委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 ...

  • C#委托Delegate和事件Event实战应用

    一、委托的概念 C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。委托(Delegate)是...

  • C# 委托(Delegate)

    C# 中的委托(Delegate)类似于 C 或 C++ 中的函数指针。委托(Delegate) 是存有对某个方法...

  • 19-委托

    C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。 委托(Delegate) 是存有对某个方...

  • C# 委托(delegate)

    委托是一种引用类型,可以将方法作为参数传递给其他方法,作为参数的这个方法可以是静态方法,实例方法,也可以是匿名方法...

  • C# delegate

    C# 中的 Delegate 类似于 C++ 中函数的指针。所有的委托Delegate都派生自 System.De...

  • 关于C#中的委托与事件以及两者之间的关系

    一 关于委托 1.委托的概念: C# 中的委托(Delegate)是一种引用类型变量,它类似于C的函数指针,...

  • C#之delegate(委托)

    定义 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做...

  • 52个有效方法(23) - 通过委托与数据协议进行对象间的通信

    委托模式(Delegate pattern) 委托模式(Delegate pattern):用来实现对象间的通信 ...

网友评论

      本文标题:C# 委托(Delegate)

      本文链接:https://www.haomeiwen.com/subject/tpcfzftx.html