转载以记录:http://blog.csdn.net/educast/article/details/7219854

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

 usingSystem;
usingSystem.Windows.Forms;
delegatevoid DisplayMessage(stringmessage);
publicclass TestCustomDelegate{
public static void Main()
{
DisplayMessage messageTarget;
if(Environment.GetCommandLineArgs().Length > )
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine;
messageTarget("Hello, World!");
} private static void ShowWindowsMessage(stringmessage)
{
MessageBox.Show(message);
}
}

以下简化了此代码,它所用的方法是实例化 Action<T> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。

 usingSystem;
usingSystem.Windows.Forms;
publicclass TestAction1
{
publicstatic void Main()
{
Action<string> messageTarget;
if(Environment.GetCommandLineArgs().Length > )
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 > )
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; public class TestLambdaExpression
{
public static void Main()
{
Action<string> messageTarget; if(Environment.GetCommandLineArgs().Length > )
messageTarget = s => ShowWindowsMessage(s);
else
messageTarget = s => Console.WriteLine(s); messageTarget("Hello, World!");
} private static 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
*/

最新文章

  1. Aop动态生成代理类时支持带参数构造函数
  2. mysql 截取身份证出生日期
  3. ZooKeeper:Quick Start
  4. 淘宝UWP桌面版已经发布
  5. 关于在Servelet中如何获取当前时间的操作
  6. JavaWeb---总结(九)通过Servlet生成验证码图片
  7. swing复制文本框内容
  8. MD5 加密的两种方法
  9. Eclipse中web项目的默认发布路径改为外部Tomcat中webapp路径
  10. [Hadoop源码解读](五)MapReduce篇之Writable相关类
  11. Jenkins错误“editable email notification aborted due to exception”的问题解决
  12. Java集合框架:Set、List、Map等介绍
  13. 算法笔记_015:快速排序(Java)
  14. c#委托概念
  15. 学习Jammendo代码的心路历程(一)简单的淡出效果实现
  16. U3D学习14-一阶段学习总结
  17. C语言字符串读入函数笔记
  18. [Spark][Python]DataFrame select 操作例子
  19. 【Java】 剑指offer(53-3) 数组中数值和下标相等的元素
  20. Shell 脚本合集

热门文章

  1. 【Luogu】P1013进制位(搜索)
  2. BZOJ1926 [Sdoi2010]粟粟的书架 【主席树 + 二分 + 前缀和】
  3. mybatis学习(一)——mybatis简介
  4. P1582 倒水 (二进制)
  5. 【数位DP】HDU 6156 Palindrome Function
  6. cf21D Traveling Graph
  7. scrapy介绍及源码分析
  8. 查看Linux版本的方法
  9. 树莓派LED指示灯说明
  10. Java集合——遍历集合元素并修改