zoukankan      html  css  js  c++  java
  • 命名的方法 匿名方法 对委托进行实例化

    命名方法对委托实例




    // Declare a delegate
    delegate void Del(int i, double j); class MathClass { static void Main() { MathClass m = new MathClass(); // Delegate instantiation using "MultiplyNumbers" Del d = m.MultiplyNumbers; // Invoke the delegate object. System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':"); for (int i = 1; i <= 5; i++) { d(i, 2); } } // Declare the associated method. void MultiplyNumbers(int m, double n) { System.Console.Write(m * n + " "); } }
    匿名方法与命名方法 实例 比较
    // Declare a delegate
    delegate void Printer(string s);
    class TestClass
    {
    static void Main()
    {
    // Instatiate the delegate type using an anonymous method:
    Printer p = delegate(string j)
    {
    System.Console.WriteLine(j);
    };
    // Results from the anonymous delegate call:
    p("The delegate using the anonymous method is called.");
    // The delegate instantiation using a named method "DoWork":
    p = new Printer(TestClass.DoWork);
    // Results from the old style delegate call:
    p("The delegate using the named method is called.");
    }
    // The method associated with the named delegate:
    static void DoWork(string k)
    {
    System.Console.WriteLine(k);
    }
    }
    输出
    

    The delegate using the anonymous method is called.

    The delegate using the named method is called.

  • 相关阅读:
    multipart/form-data
    Java面试之SE基础基本数据类型
    数据库中的悲观锁和乐观锁详解
    j2SE基回顾(一)
    Hibernate 检索查询的几种方式(HQL,QBC,本地SQL,集成Spring等)
    消防(bzoj 2282)
    YY的GCD(bzoj 2820)
    Problem b(bzoj 2301)
    完全平方数(bzoj 2440)
    The Luckiest number(hdu 2462)
  • 原文地址:https://www.cnblogs.com/xiaobaigang/p/832026.html
Copyright © 2011-2022 走看看