zoukankan      html  css  js  c++  java
  • C# delegate & event

    public delegate void MyDelegate(string mydelegate);//声明一个delegate对象

    //实现有相同参数和返回值的函数
            public void HelloDelegate(string mydelegate)
            {
                Console.WriteLine(mydelegate);
            }

    MyDelegate mydelegate = new MyDelegate(testClass.HelloDelegate);//产生delegate对象
    mydelegate("Hello delegate");//调用

    From MSDN: [C# delegate的演进]

    class Test
    {
        delegate void TestDelegate(string s);
        static void M(string s)
        {
            Console.WriteLine(s);
        }
    
        static void Main(string[] args)
        {
            // Original delegate syntax required 
            // initialization with a named method.
            TestDelegate testDelA = new TestDelegate(M);
    
            // C# 2.0: A delegate can be initialized with
            // inline code, called an "anonymous method.匿名函式" This
            // method takes a string as an input parameter.
            TestDelegate testDelB = delegate(string s) { Console.WriteLine(s); };
    
            // C# 3.0. A delegate can be initialized with
            // a lambda expression. The lambda also takes a string
            // as an input parameter (x). The type of x is inferred by the compiler.
            TestDelegate testDelC = (x) => { Console.WriteLine(x); };
    
            // Invoke the delegates.
            testDelA("Hello. My name is M and I write lines.");
            testDelB("That's nothing. I'm anonymous and ");
            testDelC("I'm a famous author.");
    
            // Keep console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    /* Output:
        Hello. My name is M and I write lines.
        That's nothing. I'm anonymous and
        I'm a famous author.
        Press any key to exit.
     */

    =====Declaring an event=====   
    1, To declare an event inside a class, first a delegate type for the event must be declared, if none is already declared.
    public delegate void ChangedEventHandler(object sender, EventArgs e);
    2, Next, the event itself is declared.
    public event ChangedEventHandler Changed;
    3, Invoking an event
    if (Changed != null)
    Changed(this, e);
    4, Hooking up to an event
    • Compose a new delegate onto that field.
    // Add "ListChanged" to the Changed event on "List":
    List.Changed += new ChangedEventHandler(ListChanged);
    • Remove a delegate from a (possibly composite) field.
    // Detach the event and delete the list:
    List.Changed -= new ChangedEventHandler(ListChanged);
     
  • 相关阅读:
    FastAPI 学习之路(十)请求体的字段
    FastAPI 学习之路(九)请求体有多个参数如何处理?
    FastAPI 学习之路(八)路径参数和数值的校验
    FastAPI 学习之路(七)字符串的校验
    FastAPI 学习之路(六)查询参数,字符串的校验
    【2021-09-09】被人说自大是有原因的
    【2021-09-08】连岳摘抄
    【2021-09-07】管理工作,得上下同步
    【2021-09-06】连岳摘抄
    【2021-09-05】连岳摘抄
  • 原文地址:https://www.cnblogs.com/chayu3/p/3442122.html
Copyright © 2011-2022 走看看