深入C#笔记(一)---Action
来源:互联网 发布:java中createNewfile 编辑:程序博客网 时间:2024/05/22 08:05
Action的使用
Action<T> Delegate
概述:Encapsulates a method that has a single parameter and does not return a value.
描述:You can use the Action<T> delegate to pass a method as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate. This means that the encapsulated method must have one parameter that is passed to it by value, and it must not return a value. (In C#, the method must return void. Typically, such a method is used to perform an operation.
个人理解:
- 用一种更简洁的方式创建委托
- 作为IEnumerable<T>某些方法的参数,例如Forecach,
- 创建Acton实例可以用传统的委托方法,也可以用Lambda表达式,当然很多时候是我们可以不显示创建一个Action,但实际上我们要知道,创建的是Action对象
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); }}
使用委托以及Lambda的方式:
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); }; //Lambda 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); }}
- 深入C#笔记(一)---Action
- C#学习笔记一:深入HelloWorld内部
- 深入.net笔记(一)
- 《Junit In Action》学习笔记(一)
- AJAX IN ACTION 学习笔记(一)
- JUnit in action学习笔记(一)
- Liferay in action 学习笔记(一)
- spring in action 学习笔记(一)
- c# 笔记(一)
- c#笔记(一)
- 【web开发学习笔记】Structs2 Action学习笔记(一)
- c#深入学习笔记
- C#委托介绍(delegate、Action、Func、predicate)(一)
- 《spring in action》笔记(一)
- 《深入解析ATL》笔记(一)
- 《VC++深入详解》笔记(一)
- 深入理解计算机学习笔记(一)
- apche深入学习笔记(一)
- 骨骼动画系统Cal3D
- IDIOPEN 2011 B - Sheep Frenzy
- 零零散散学算法之详解数据压缩算法(上)
- 学习MongoDB--(10-2):分片(建立分片)
- HDU 1032 The 3n + 1 problem
- 深入C#笔记(一)---Action
- MySQL中处理Null时要注意两大陷阱
- Android应用开发之(按需加载View)
- IE、FF、Safari、OP不同浏览器兼容报告
- Cla3D骨骼动画源代码所需环境配置
- poj1062最短路
- 构建自己的debian软件包仓库
- Protel与PADS之间相关文件的转换
- 骨骼动画反向动力学(IK)的实现