Func 委托 和 Action 委托 初步谈论
继上篇EventHandler之后,继续填坑,简单了解下Func<TResult> 委托 和 Action 委托.
msdn对于两者的解释:
Func<TResult>:封装一个不具有参数但却返回 TResult 参数指定的类型值的方法。
Action:封装一个方法,该方法不具有参数并且不返回值。
两者的区别在于:有无返回值。
至于 Func<T,TResult>、Func<T1,T2,TResult>....
Action<T>、Action<T1, T2>.... ,就是参数个数的区别了 。
借用msdn上面的例子来说明两者的使用方式:
Func<TResult>:
using System;
using System.IO; public class TestDelegate
{
public static void Main()
{
OutputTarget output = new OutputTarget();
Func<bool> methodCall = output.SendToFile;
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
} public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
您可以按照以下示例所演示的那样在 C# 中将 Func<TResult> 委托与匿名方法一起使用。
using System;
using System.IO; public class Anonymous
{
public static void Main()
{
OutputTarget output = new OutputTarget();
Func<bool> methodCall = delegate() { return output.SendToFile(); };
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
} public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Func<T, TResult> 委托。
using System;
using System.IO; public class Anonymous
{
public static void Main()
{
OutputTarget output = new OutputTarget();
Func<bool> methodCall = () => output.SendToFile();
if (methodCall())
Console.WriteLine("Success!");
else
Console.WriteLine("File write operation failed.");
}
} public class OutputTarget
{
public bool SendToFile()
{
try
{
string fn = Path.GetTempFileName();
StreamWriter sw = new StreamWriter(fn);
sw.WriteLine("Hello, World!");
sw.Close();
return true;
}
catch
{
return false;
}
}
}
再来看 Action<> 的使用:
using System;
using System.Windows.Forms; public class Name
{
private string instanceName; public Name(string name)
{
this.instanceName = name;
} public void DisplayToConsole()
{
Console.WriteLine(this.instanceName);
} public void DisplayToWindow()
{
MessageBox.Show(this.instanceName);
}
} public class testTestDelegate
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = testName.DisplayToWindow;
showMethod();
}
}
您也可以按照以下示例所演示的那样在 C# 中将 Action 委托与匿名方法一起使用。
using System;
using System.Windows.Forms; public class Name
{
private string instanceName; public Name(string name)
{
this.instanceName = name;
} public void DisplayToConsole()
{
Console.WriteLine(this.instanceName);
} public void DisplayToWindow()
{
MessageBox.Show(this.instanceName);
}
} public class Anonymous
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = delegate() { testName.DisplayToWindow();} ;
showMethod();
}
}
您也可以按照以下示例所演示的那样将 lambda 表达式分配给 Action 委托实例。
using System;
using System.Windows.Forms; public class Name
{
private string instanceName; public Name(string name)
{
this.instanceName = name;
} public void DisplayToConsole()
{
Console.WriteLine(this.instanceName);
} public void DisplayToWindow()
{
MessageBox.Show(this.instanceName);
}
} public class LambdaExpression
{
public static void Main()
{
Name testName = new Name("Koani");
Action showMethod = () => testName.DisplayToWindow();
showMethod();
}
}
慢慢积累,加油
Func 委托 和 Action 委托 初步谈论的更多相关文章
- C#中常见的委托(Func委托、Action委托、Predicate委托)
今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通 ...
- [C#学习笔记]Func委托与Action委托
学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!好的我的前言就是这个. Fun 如果要用有输入参数,有返回值的委托,那么Func委托 ...
- C#内置泛型委托:Action委托
1.什么是Action泛型委托 Action<T>是.NET Framework内置的泛型委托,可以使用Action<T>委托以参数形式传递方法,而不用显示声明自定义的委托.封 ...
- .NET : Func委托和Action委托
其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: public delegate void myDeleg ...
- Func委托和Action委托
http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate The difference between Func a ...
- 委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解
1.使用简单委托 namespace 简单委托 { class Program { //委托方法签名 delegate void MyBookDel(int a); //定义委托 static MyB ...
- C#系统委托之Action And Func
Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...
- Func<T>与Action<T>委托泛型介绍:转
.Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...
- Func<T>与Action<T>委托泛型介绍
.Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...
随机推荐
- Linq to Entities不识别方法
db.UserValidates.Include(a => a.User).Where(uv => u.UserValidates.Contains(uv, c)).ToList(); 执 ...
- WebApi2官网学习记录--HTTP Message Handlers
Message Handlers是一个接收HTTP Request返回HTTP Response的类,继承自HttpMessageHandler 通常,一些列的message handler被链接到一 ...
- 将html导出到excel或word
本质是将html写成word或excel支持的html格式. 如何将html写成word或excel支持的格式? 只需打开计算机上任意一个word或excel文档,打开文件->另存为,选择文件类 ...
- 用C#代码控制水晶报表中的对象
在C#代码中调用水晶报表的各个对象:字段对象:FieldObject obj=(FieldObject)oRpt.ReportDefinition.ReportObjects["FieldO ...
- Invalid content was found starting with element 'taglib'”
今天在使用struts-menu制作菜单,在web.xml中写入 <taglib> <taglib-uri>/WEB-INF/struts-menu.tld</ ...
- 浅谈Windows Server APPFABRIC
hi,everyone !真的是好久好久没有update blog了,因为最近忙着备考,没有时间对<数据结构与算法>进行研究学习了.所以,blog一直未更新.today is Friday ...
- xampp集成安装的mysql修改密码(Window)
把mysql安装目录bin文件夹加入环境变量 path:mysqlPath\bin; 或者进入mysql安装目录bin文件夹下,按住shift键盘鼠标右击进入命令行 键入命令 mysqladmin - ...
- RegExp子模式- "()"
读书笔记 把JavaScript权威指南拿出来瞅瞅,正巧看到了第十章 正则表达式的模式匹配 最初接触js的时候,基本上都是在做验证.什么数字验证.命名验证.身份证格式验证.电话号码验证.都是用正则表达 ...
- SDOI HH的项链 HEOI采花
题目大意: SDOI求一个区间内只出现一次的数的个数.多组询问. HEOI 求一个区间内出现至少两次的数的个数.多组询问. SDOI HH'neckplace如果每次询问都是1..r的话,那么我们只要 ...
- C++读取一串不知个数的数字
#include <iostream> using namespace std; int main(){ ]; ; while(cin>>shuzu[i]){ i++; } ; ...