zoukankan      html  css  js  c++  java
  • C# Delegate 使用

    Delegate, Event
    Delegate其实就相当于C++/C里面的函数指针.用法如下:
    1.最常规的使用
        class Program
        {
            public delegate void NumberChangedEventHandler(int number);
            class Publisher
            {
                public NumberChangedEventHandler NumberChanged;  
            }
            class Subscriber
            {
                public void OnNumberChanged(int number)
                {
                    string message = "New value is " + number;
                    Console.WriteLine(message);
                }
            }
            static void Main(string[] args)
            {
                Publisher pub = new Publisher();
                Subscriber sub1 = new Subscriber();
                Subscriber sub2 = new Subscriber();
                pub.NumberChanged += new NumberChangedEventHandler(sub1.OnNumberChanged);
                pub.NumberChanged += new NumberChangedEventHandler(sub2.OnNumberChanged);
    
                pub.NumberChanged(3);
                Console.WriteLine("over");
            }
        }
    2.直接当成函数指针用
        class Program
        {
            public delegate void NumberChangedEventHandler(int number);
            class Publisher
            {
                public NumberChangedEventHandler NumberChanged;  
            }
            class Subscriber
            {
                public void OnNumberChanged(int number)
                {
                    string message = "New value is " + number;
                    Console.WriteLine(message);
                }
            }
            static void Main(string[] args)
            {
                Publisher pub = new Publisher();
                Subscriber sub1 = new Subscriber();
                pub.NumberChanged = sub1.OnNumberChanged;
                pub.NumberChanged(3);
            }
        }
    3.异步调用
        class Program
        {
            public delegate void NumberChangedEventHandler(int number);
            class Publisher
            {
                public NumberChangedEventHandler NumberChanged;  
            }
            class Subscriber
            {
                public void OnNumberChanged(int number)
                {
                    string message = "New value is " + number;
                    Console.WriteLine(message);
                }
            }
            static void Main(string[] args)
            {
                Publisher pub = new Publisher();
                Subscriber sub1 = new Subscriber();
                pub.NumberChanged += new NumberChangedEventHandler(sub1.OnNumberChanged);
                pub.NumberChanged.BeginInvoke(3, null, null);
            }
        }

    如果这篇文章对您有帮助,您可以打赏我

    技术交流QQ群:15129679

  • 相关阅读:
    原创的java数据访问框架
    在ASP.NET中使用Session常见问题集锦
    Infragistics中WebGrid的MultiColumn Headers设计
    常用asp.net代码
    如何实现函数IF的嵌套超过七层?
    Microsoft® Visual Studio® 2005 Team Suite Service Pack 1
    ASP.NET中常用的文件上传下载方法
    ASP.NET 2.0:使用用户控件和定制的Web部件个人化你的门户网站
    office2007TW
    http://www.ydowns.com/download/53279.rar
  • 原文地址:https://www.cnblogs.com/yeminglong/p/2442595.html
Copyright © 2011-2022 走看看