C# Action<T> 使用
2012-01-30 15:16
330 查看
在使用 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
*/
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
*/
相关文章推荐
- C#_delegate - Pair<T> & 简单顺序逆序 & 方法委托(在Pair类下)&枚举类型 混搭使用
- C#基础学习日志===&amp;amp;amp;amp;gt;命名空间的使用&amp;amp;amp;amp;lt;===
- FloatingActionButton(悬浮按钮)使用学习<一>
- C#基础:委托之Action<T>和Func<T>的用法(转)
- C# 笔记 Func<TResult> 委托、Action<T> 委托
- 深入理解 c# 第五章 匿名方法用于Action<T>委托类型 反转字符 求平方根 求平均数
- 关于c#中的Func<>和Action<>
- jsp页面,使用Struts2标签,传递和获取Action类里的参数,注意事项。<s:a action><s:iterator><s:param>ognl表达式 在编写SSH2项目的时候,除
- android studio <item/>标签下不能使用showAsAction属性
- 《Effective C#》读书笔记——条目1:使用属性而不是可访问的数据成员<C#语言习惯>
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- C#基础:委托之Action<T>和Func<T>的用法
- Struts2:<s:action>的使用
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [转]C# 中使用委托对List<>进行排序和筛选
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- [C#] 泛型委派 Action<T> 與 Func<T>
- FloatingActionButton(悬浮按钮)使用学习<一>
- <C#>_使用代码编辑控件属性的一些小技巧
- C# Action<T>、Func<T>、Predicate<T>委托的区别、用法