扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
你可以把匿名方法想象为一个实现与委托进行关联这项功能的便捷途径。如果同时看一下匿名方法实现和命名方法实现所取得IL结果,你会发现这两者之间的差别非常小。当编译器碰到匿名方法的时候,它会在类里面创建一个命名方法,并将它与委托进行关联。所以匿名方法在运行期间与命名方法的性能非常类似——性能的增加体现在开发人员的生产效率上,而不是运行期间的执行上。
如何使用匿名方法
多用代码少用说教来解释和理解匿名方法会容易一些。下面的例子应该能够说明你自己可以如何利用匿名方法的优势:
例1——基础知识
使用匿名方法很简单。你只需要把匿名方法放在你通常放命名方法的关联语句里。在下面这个例子里,我把匿名方法和示例1的委托关联在一起:
示例列表1
    #region Simple example - Example1
    privatedelegatevoidExample1();
    privatevoid btnExample1_Click(object sender, EventArgs e)
    {
        //Declare an instance of the Example1 delegate.
        // You can see where I'm using the anonymous
        // method in place of a named method - it follows
        // the delegate() keyword.
        Example1 example =
            newExample1(
                delegate()
                {
                    MessageBox.Show("Example1");
                });
        example();
    }
    #endregion
例2——变量范围
任何在匿名方法里声明的变量的范围都不会超出匿名方法的代码块。但是,匿名方法确实具有访问它们代码块之外的变量的能力,只要这些变量在匿名方法所使用的范围里。这些变量被微软称为外部变量。下面示例2显示了匿名方法如何引用外部变量:
示例列表2
   #region Variable scope example - Example2
   privatedelegatevoidExample2();
   privatevoid btnExample2_Click(object sender, EventArgs e)
   {
       //Setup our parameters.
       string firstName = "Zach";
       string lastName = "Smith";
       //Create an instance of the Example2 delegate with an
       // anonymous method.
       Example2 example = 
           newExample2(
               delegate() { 
                   MessageBox.Show(firstName + " " + lastName); 
              });
       //Execute the delegate.
       example();
   }
   #endregion
要注意的是,根据MSDN的文档,匿名方法里的ref和out参数无法被访问到。
例3——参数的传递
你可以将参数传递给匿名方法,方式就和你处理引用命名方法参数的委托一样。下面的示例3说明这种类型的功能:
示例列表3
   #region Parameter example - Example3
   privatedelegatevoidExample3(string firstName, string lastName);
   privatevoid btnExample3_Click(object sender, EventArgs e)
   {
       //Setup our parameters.
       string parameter1 = "Zach";
       string parameter2 = "Smith";
       //Create an instance of the Example3 delegate with an
       // anonymous method.
       Example3 example =
           newExample3(
               delegate(string firstName, string lastName)
               {
                   MessageBox.Show("Example3: " + firstName + " " + lastName);
               });
       //Execute the delegate.
       example(parameter1, parameter2);
   }
   #endregion
例4——多个方法的关联
就和命名方法一样,将多个匿名方法与同一个委托进行关联是可能的。这在很多情况下会非常有用——首先想到的是把一个简单的处理程序添加给按钮的点击事件。下面的代码(示例4)显示了一个委托,它同时带有与之相关联一个匿名方法和一个命名方法:
示例列表4
   #region Multiple method association (stacking) - Example4
   privatedelegatevoidExample4(string firstName, string lastName);
   privatevoid btnExample4_Click(object sender, EventArgs e)
   {
       //Setup our parameters.
       string parameter1 = "Zach";
       string parameter2 = "Smith";
       //Create an instance of the Example4 delegate with an
       // anonymous method.
       Example4 example =
           newExample4(
               delegate(string firstName, string lastName)
               {
                   MessageBox.Show("Example4: " + firstName + " " + lastName);
               });
       //Add another method to the delegate - this time
       // a named method.
       example += newExample4(Example4NamedMethod);
       //Execute the delegate.
       example(parameter1, parameter2);
   }
   privatevoid Example4NamedMethod(string firstName, string lastName)
   {
       MessageBox.Show("Example4Method: " + firstName + " " + lastName);
   }
   #endregion
例5——将匿名方法作为参数传递
就和命名方法一样,将匿名方法作为参数传递给函数是可能的。这并不是一个我认为会通常使用的特性,但是我敢肯定未来会有这种需要。下面的代码(例5)说明了这种类型的功能,它将一个命名方法作为参数传递给了函数:
示例列表5
   #region Passing anonymous methods - Example5
   privatedelegatevoidExample5(string firstName, string lastName);
   privatevoid btnExample5_Click(object sender, EventArgs e)
   {
       //Execute Passit and pass the anonymous method.
       Passit((Example5)delegate(string firstName, string lastName)
               {
                   MessageBox.Show("Example5: " + firstName + " " + lastName);
               });
       //Execute Passit with the named method.
       Passit(Example5NamedMethod);
   }
   privatevoid Example5NamedMethod(string firstName, string lastName)
   {
       MessageBox.Show("Example5Method: " + firstName + " " + lastName);
   }
   privatevoid Passit(Example5 example)
   {
       example("Zach", "Smith");
   }
   #endregion
例6—-访问类成员
这是对上面例2的变量范围的扩展。但是,这个例子(例6)说明了匿名参数还能够在它们的代码块之外执行命名方法:
示例列表6
    #region Accessing class members - Example6
    privatedelegatevoidExample6();
    privateint _customerId;
    privatestring _customerCode;
    publicint CustomerID
    {
        get { return _customerId; }
        set { _customerId = value; }
    }
    publicstring CustomerCode
    {
        get { return _customerCode; }
        set { _customerCode = value; }
    }
    privatevoid btnExample6_Click(object sender, EventArgs e)
    {
        //Populate out properties.
        this.CustomerID = 90;
        this.CustomerCode = "1337HK";
        //Setup the delegate/anonymous method.
        Example6 example =
            newExample6(
                delegate
                {
                    this.ShowCustomer(this.CustomerID, this.CustomerCode);
                });
        //Execute the delegate.
        example();
        //Change the properties.
        this.CustomerID = 54;
        this.CustomerCode = "L4M3";
        //Execute the delegate again.
        // Notice that the new values are reflected.
        example();
    }
    privatevoid ShowCustomer(int customerId, string customerCode)
    {
        MessageBox.Show(
            String.Format("CustomerID: Customer Code: ",
                            customerId, customerCode));
    }
    #endregion
要注意的是,我两次调用了与匿名方法相关联的委托。你可能会发现一个很有趣的事情:在这些调用中,方法会输出两组不同的值。这是因为用在匿名方法里的外部变量在创建匿名方法的时候被引用。这意味着对这些变量的任何更改都会在匿名函数访问变量的时候被反映出来。
你可能还注意到在这个实例里委托关键字后面没有括号。当匿名方法不需要带参数的时候,后面的括号是可选的。
评论
我希望本文已经说明如何使用匿名方法。虽然它们还不是革命性的,但是它们是C#演化成为一门程序员友好的语言过程中的一个重要步骤。
(文/Zach Smith,builder.com.com)
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。