.NET中的委托——摘自MSDN
封装一个方法,该方法只有一个参数并且不返回值。
命名空间: System
程序集: mscorlib(在 mscorlib.dll 中)
可以使用 Action<T> 委托以参数形式传递方法,而不用显式声明自定义的委托。 封装的方法必须与此委托定义的方法签名相对应。 也就是说,封装的方法必须具有一个通过值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void。 在 Visual Basic 中,必须通过 Sub…End Sub 结构来定义它。 它也可以是返回已忽略的值的方法。) 通常,这种方法用于执行某个操作。
说明 |
|---|
|
若要引用具有一个参数并返回值的方法,请改用泛型 Func<T, TResult> 委托。 |
在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的方法的委托。 例如,以下代码显式声明了一个名为 DisplayMessage 的委托,并将对 WriteLine 方法或 ShowWindowsMessage 方法的引用分配给其委托实例。
using System;
using System.Windows.Forms; delegate void DisplayMessage(string message); public class TestCustomDelegate
{
public static void Main()
{
DisplayMessage messageTarget; if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine; messageTarget("Hello, World!");
} private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
以下示例简化了此代码,它所用的方法是实例化 Action<T> 委托,而不是显式定义一个新委托并将命名方法分配给该委托。
using System;
using System.Windows.Forms; public class TestAction1
{
public static void Main()
{
Action<string> messageTarget; if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = ShowWindowsMessage;
else
messageTarget = Console.WriteLine; messageTarget("Hello, World!");
} private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
您也可以按照以下示例所演示的那样在 C# 中将 Action<T> 委托与匿名方法一起使用。 (有关匿名方法的简介,请参见匿名方法(C# 编程指南)。)
using System;
using System.Windows.Forms; public class TestAnonMethod
{
public static void Main()
{
Action<string> messageTarget; if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = delegate(string s) { ShowWindowsMessage(s); };
else
messageTarget = delegate(string s) { Console.WriteLine(s); }; messageTarget("Hello, World!");
} private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Action<T> 委托实例。 (有关 lambda 表达式的简介,请参见 Lambda 表达式(C# 编程指南)。)
using System;
using System.Windows.Forms; public class TestLambdaExpression
{
public static void Main()
{
Action<string> messageTarget; if (Environment.GetCommandLineArgs().Length > 1)
messageTarget = s => ShowWindowsMessage(s);
else
messageTarget = s => Console.WriteLine(s); messageTarget("Hello, World!");
} private static void ShowWindowsMessage(string message)
{
MessageBox.Show(message);
}
}
ForEach 和 ForEach<T> 方法都采用 Action<T> 委托作为参数。 通过使用由委托封装的方法,可以对数组或列表中的每个元素执行操作。 此示例使用 ForEach 方法提供说明。
下面的示例演示如何使用 Action<T> 委托来打印 List<T> 对象的内容。 在此示例中,使用 Print 方法将列表的内容显示到控制台上。 此外,C# 示例还演示如何使用匿名方法将内容显示到控制台上。 请注意该示例不显式声明 Action<T> 变量。 相反,它传递方法的引用,该方法采用单个参数而且不将值返回至 List<T>.ForEach 方法,其单个参数是一个 Action<T> 委托。 同样,在 C# 示例 中,Action<T> 委托不被显式地实例化,因为匿名方法的签名匹配 List<T>.ForEach 方法所期望的 Action<T> 委托的签名。
using System;
using System.Collections.Generic; class Program
{
static void Main()
{
List<String> names = new List<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);
});
} private static void Print(string s)
{
Console.WriteLine(s);
}
}
/* This code will produce output similar to the following:
* Bruce
* Alfred
* Tim
* Richard
* Bruce
* Alfred
* Tim
* Richard
*/
.NET中的委托——摘自MSDN的更多相关文章
- C# 中的委托和事件
觉得这篇文章写的非常好,大神之作,由简入繁,对我这种初学者来说帮忙很大,特此留存下. 摘自:http://tracefact.net/CSharp-Programming/Delegates-and- ...
- 编写高质量代码改善C#程序的157个建议——建议36:使用FCL中的委托声明
建议36:使用FCL中的委托声明 FCL中存在3类这样的委托声明,它们分别是:Action.Func.Predicate.尤其是在它们的泛型版本出来以后,已经能够满足我们在实际编码过程中的大部分需求. ...
- Objective-C中的委托(代理)模式
我个人更喜欢把委托(Delegate)模式称为代理(Proxy)模式.还是那句话,第一次接触代理模式是在Java中接触的,在Java中实现代理模式和接口是少不了的.当时学习Spring的时候用到了接口 ...
- C# 中的委托和事件(转)
引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...
- C# 中的委托和事件(转载)
引言 委托 和 事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去 ...
- 【转】C# 中的委托和事件
阅读目录 C# 中的委托和事件 引言 将方法作为方法的参数 将方法绑定到委托 事件的由来 事件和委托的编译代码 委托.事件与Observer设计模式 .Net Framework中的委托与事件 总结 ...
- 第3章 C#中的委托和事件
.NET框架中的委托和事件 using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 分分钟用上C#中的委托和事件之窗体篇
上次以鸿门宴的例子写了一篇名为<分分钟用上C#中的委托和事件>的博文,旨在帮助C#初学者迈过委托和事件这道坎,能够用最快的速度掌握如何使用它们.如果觉得意犹未尽,或者仍然不知如何在实际应用 ...
- 《C#高级编程》学习笔记------C#中的委托和事件(续)
本文转载自张子阳 目录 为什么要使用事件而不是委托变量? 为什么委托定义的返回值通常都为void? 如何让事件只允许一个客户订阅?(事件访问器) 获得多个返回值与异常处理 委托中订阅者方法超时的处理 ...
随机推荐
- ubuntu14.04 swap not avalible交换分区不能使用
系统最近特别卡,打开"System monitor"中的resource发现"swap not avalibe".原来系统每交换分区. 我的是笔记本电脑,存储空间有限.首先我下载磁盘分区工具Gpart ...
- HW4.31
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- Microsoft Dynamics CRM 2011 相关-摘自网络
Microsoft Dynamics CRM Server 2011硬件需求: 组件 *最低要求 *推荐配置 处理器 x64 体系结构或兼容的双核 1.5 GHz 处理器 四核 x64 体系结构 2 ...
- hdoj 2502 月之数
月之数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Swift中UITableView的简单使用
Swift中的注释 使用"// MARK:- 注释内容",对属性或方法进行注释 使用"///注释内容"对属性或方法提供调用说明的注释 使用extension对同 ...
- jQuery获取鼠标事件源(万能)
//任意位置 $(document).ready(function(){ $(document).click(function(){ $("#id_").hide(); }); } ...
- Spring源代码解析(收藏)
Spring源代码解析(收藏) Spring源代码解析(一):IOC容器:http://www.iteye.com/topic/86339 Spring源代码解析(二):IoC容器在Web容器中的 ...
- Linux下源码安装Nginx服务
nginx 安装 linux 系统需要安装必备的开发包,比如 gcc,gcc-c++ 1. openssl (支持 https) https://www.openssl.org/source/ ...
- 阿里云部署Docker(7)----将容器连接起来
路遥知马力.日久见人心.恩. 该坚持的还是要坚持. 今天看到一个迅雷的师弟去了阿里,祝福他,哎,尽管老是被人家捧着叫大牛.我说不定通过不了人家的面试呢.哎,心有惭愧. 本文为本人原创,转载请表明来源: ...
- [每日一题] 11gOCP 1z0-052 :2013-08-31 数据库的存储结构....................................................A8
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/10784599 . 正确答案:A 将逻辑存储与物理存储分开是关系数据库范例的必要部分.关系数 ...
说明