C# Action<T> 使用
来源:互联网 发布:stm32f103rb数据手册 编辑:程序博客网 时间:2024/06/06 13:19
在使用 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# Action<T> 使用
- C# Action<T> 委托
- C# Action<T> 委托
- C# Action<T>
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- C#委托Action、Action<T>、Func<T>、Predicate<T>
- Action<T> Delegate 使用
- C#委托之 Action<T>委托
- c#中Action<T>和Func<T>委托
- C#中的Action<T>、Func<T>委托和Lambda表达式
- C#中 Action<T>和Func<T>的用法
- C# Func<T>、Action<T> 的区别于说明
- C# Action<T>、Func<T>、Predicate<T>委托的区别、用法
- C# List<T>使用方式
- C# List<T>使用方式
- t:jscookMenu t:panelNavigation2 及 t:jscookMenu不能够使用action跳转
- C#基础:委托之Action<T>和Func<T>的用法
- Action<T>、Func<T>委托
- Linux中ln连接命令的用法
- 转载-TCP/IP模型的一个简单解释
- CSDN精选Android开发博客
- struts.properties
- 类对象的内存分配(一)
- C# Action<T> 使用
- ubuntu下安装pthread的manpages(man 手册)
- MySQL之Explain
- 内页链接锚文本建设的3个小问题
- 想要答案,请Google,想要知识,请看书
- Fastboot - elinux.org
- Action<T>和Func<T>委托
- 常用正则表达式
- Java文件操作新问题如何解决记事本的换行