zoukankan      html  css  js  c++  java
  • 匿名方法

        在 2.0 之前的 C# 版本中,声明委托的唯一方法是使用命名方法。C# 2.0 引入了匿名方法。
    要将代码块传递为委托参数,创建匿名方法则是唯一的方法。例如:

    // Create a handler for a click event
    button1.Click += delegate(System.Object o, System.EventArgs e)
                       
    { System.Windows.Forms.MessageBox.Show("Click!"); };

    // Create a delegate instance
    delegate void Del(int x);

    // Instantiate the delegate using an anonymous method
    Del d = delegate(int k) /*  */ };

    如果使用匿名方法,则不必创建单独的方法,因此减少了实例化委托所需的编码系统开销。

    例如,如果创建方法所需的系统开销是不必要的,在委托的位置指定代码块就非常有用。启动新线程即是一个很好的示例。无需为委托创建更多方法,线程类即可创建一个线程并且包含该线程执行的代码。

    void StartThread()
    {
        System.Threading.Thread t1 
    = new System.Threading.Thread
          (
    delegate()
                
    {
                    System.Console.Write(
    "Hello, ");
                    System.Console.WriteLine(
    "World!");
                }
    );
        t1.Start();
    }

    备注

    匿名方法的参数的范围是 anonymous-method-block

    在目标在块外部的匿名方法块内使用跳转语句(如 gotobreakcontinue)是错误的。在目标在块内部的匿名方法块外部使用跳转语句(如 gotobreakcontinue)也是错误的。

    如果局部变量和参数的范围包含匿名方法声明,则该局部变量和参数称为该匿名方法的外部变量或捕获变量。例如,下面代码段中的 n 即是一个外部变量:

    int n = 0;
    Del d 
    = delegate() { System.Console.WriteLine("Copy #:{0}"++n); };

    与局部变量不同,外部变量的生命周期一直持续到引用该匿名方法的委托符合垃圾回收的条件为止。对 n 的引用是在创建该委托时捕获的。

    匿名方法不能访问外部范围的 refout 参数。

    anonymous-method-block 中不能访问任何不安全代码。

    代码实例:

    下面的示例演示实例化委托的两种方法:

    // 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.


  • 相关阅读:
    响应式设计
    Flex box 弹性盒子布局
    下拉菜单  -
    html使用技巧
    nth-of-type(n)
    Js中 关于top、clientTop、scrollTop、offsetTop
    仿iPhone滑屏操作
    网页超过一页 点击回到顶部
    linux svn使用
    Linux查看CPU和内存使用情况
  • 原文地址:https://www.cnblogs.com/abcdwxc/p/965185.html
Copyright © 2011-2022 走看看