• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#中ActionT委托的简单使用(转)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的方法的委托。以下代码显式声明了一个名为 DisplayMessage 的委托,并将对 WriteLine 方法或 ShowWindowsMessage 方法的引用分配给其委托实例。 


usingSystem;
usingSystem.Windows.Forms; 
delegatevoid DisplayMessage(stringmessage); 
publicclass TestCustomDelegate{
   publicstatic void Main()
   {
      DisplayMessage messageTarget;  
      if(Environment.GetCommandLineArgs().Length > 1)
         messageTarget = ShowWindowsMessage;
      else
         messageTarget = Console.WriteLine; 
      messageTarget("Hello, World!");  
   }     
 
   privatestatic void ShowWindowsMessage(stringmessage)
   {
      MessageBox.Show(message);     
   }
}
 
以下简化了此代码,它所用的方法是实例化 Action<T> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。


usingSystem;
usingSystem.Windows.Forms; 
publicclass TestAction1
{
   publicstatic void Main()
   {
      Action<string> messageTarget; 
      if(Environment.GetCommandLineArgs().Length > 1)
         messageTarget = ShowWindowsMessage;
      else
         messageTarget = Console.WriteLine; 
      messageTarget("Hello, World!");  
   }     
 
   privatestatic void ShowWindowsMessage(stringmessage)
   {
      MessageBox.Show(message);     
   }
}
 
也可以将 Action<T> 委托与匿名方法一起使用。


usingSystem;
usingSystem.Windows.Forms;
 
publicclass TestAnonMethod
{
   publicstatic void Main()
   {
      Action<string> messageTarget;  
      if(Environment.GetCommandLineArgs().Length > 1)
         messageTarget = delegate(strings) { ShowWindowsMessage(s); };
      else
         messageTarget = delegate(strings) { Console.WriteLine(s); }; 
      messageTarget("Hello, World!");
   }
 
   privatestatic void ShowWindowsMessage(stringmessage)
   {
      MessageBox.Show(message);     
   }
}
 
也可以将 lambda 表达式分配给 Action<T> 委托实例。


usingSystem;
usingSystem.Windows.Forms;
 
publicclass TestLambdaExpression
{
   publicstatic void Main()
   {
      Action<string> messageTarget; 
 
      if(Environment.GetCommandLineArgs().Length > 1)
         messageTarget = s => ShowWindowsMessage(s); 
      else
         messageTarget = s => Console.WriteLine(s);
 
      messageTarget("Hello, World!");
   }
 
   privatestatic void ShowWindowsMessage(stringmessage)
   {
      MessageBox.Show(message);     
   }
}
 
下面使用 Action<T> 委托来打印 List<T> 对象的内容。 使用 Print 方法将列表的内容显示到控制台上。 此外还使用匿名方法将内容显示到控制台上。 请注意该示例不显式声明 Action<T> 变量。 相反,它传递方法的引用,该方法采用单个参数而且不将值返回至 List<T>.ForEach 方法,其单个参数是一个 Action<T> 委托。 同样,在 C# 示例 中,Action<T> 委托不被显式地实例化,因为匿名方法的签名匹配 List<T>.ForEach 方法所期望的 Action<T> 委托的签名。 


usingSystem;
usingSystem.Collections.Generic;
 
classProgram
{
    staticvoid Main()
    {
        List<String> names = newList<String>();
        names.Add("Bruce");
        names.Add("Alfred");
        names.Add("Tim");
        names.Add("Richard");
 
        // Display the contents of the list using the Print method.
        names.ForEach(Print);
 
        // The following demonstrates the anonymous method feature of C#
        // to display the contents of the list to the console.
        names.ForEach(delegate(String name)
        {
            Console.WriteLine(name);
        });
    }
 
    privatestatic void Print(strings)
    {
        Console.WriteLine(s);
    }
}
/* This code will produce output similar to the following:
 * Bruce
 * Alfred
 * Tim
 * Richard
 * Bruce
 * Alfred
 * Tim
 * Richard
 */


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Visual C#常用函数和方法集汇总发布时间:2022-07-13
下一篇:
C# 调用 WebService 连接ORACLE 11g发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap