zoukankan      html  css  js  c++  java
  • C# 声明、实例化和使用委托的几种方法

     1.在 C# 1.0 及更高版本中,可以按以下示例所示声明委托。

    // Declare a delegate.
    delegate void Del(string str);
    
    // Declare a method with the same signature as the delegate.
    static void Notify(string name)
    {
        Console.WriteLine("Notification received for: {0}", name);
    } 
    // Create an instance of the delegate.
    Del del1 = new Del(Notify);
    // 调用委托:
    del1("参数");

      

    2.C# 2.0 提供了更简单的方法来编写上面的声明,如以下示例所示。

    // Instantiate Del by using an anonymous method.
    Del del3 = delegate(string name)
    { Console.WriteLine("Notification received for: {0}", name); };

    //调用委托:
    del3("参数");

    3.在 C# 3.0 及更高版本中,还可以使用 Lambda 表达式来声明和实例化委托,如以下示例所示。

    // Instantiate Del by using a lambda expression.
    Del del4 = name =>  { Console.WriteLine("Notification received for: {0}", name); };
    //调用委托:
    del4("参数");
  • 相关阅读:
    rocketmq的broker如何同步信息的?
    SO_LINGER 选项
    哈哈哈
    NAG博客目录
    事后分析$eta$
    项目展示$eta$
    测试报告$eta$
    发布声明$eta$
    Scrum meeting 10
    技术博客6--Django实现列表分页
  • 原文地址:https://www.cnblogs.com/coolsxh/p/4914840.html
Copyright © 2011-2022 走看看