首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

如何充分利用C#匿名方法的平台优势

例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)

上一页  [1] [2] 

【责编:Peng】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航