继上篇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 委托 初步谈论的更多相关文章

  1. C#中常见的委托(Func委托、Action委托、Predicate委托)

    今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通 ...

  2. [C#学习笔记]Func委托与Action委托

    学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!好的我的前言就是这个. Fun 如果要用有输入参数,有返回值的委托,那么Func委托 ...

  3. C#内置泛型委托:Action委托

    1.什么是Action泛型委托 Action<T>是.NET Framework内置的泛型委托,可以使用Action<T>委托以参数形式传递方法,而不用显示声明自定义的委托.封 ...

  4. .NET : Func委托和Action委托

    其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: public delegate void myDeleg ...

  5. Func委托和Action委托

    http://stackoverflow.com/questions/4317479/func-vs-action-vs-predicate The difference between Func a ...

  6. 委托delegate 泛型委托action<> 返回值泛型委托Func<> 匿名方法 lambda表达式 的理解

    1.使用简单委托 namespace 简单委托 { class Program { //委托方法签名 delegate void MyBookDel(int a); //定义委托 static MyB ...

  7. C#系统委托之Action And Func

    Action Action<T> Func Func<T> Action:封装一个方法,该方法不具有参数并且不返回值 public delegate void Action() ...

  8. Func<T>与Action<T>委托泛型介绍:转

    .Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...

  9. Func<T>与Action<T>委托泛型介绍

    .Net 3.5之后,微软推出了Func<T>与Action<T>泛型委托.进一步简化了委托的定义. Action<T>委托主要的表现形式如下: public de ...

随机推荐

  1. (转)SQL语句中的N'xxxx'是什么意思

    SQL语句中的N'xxxx'是什么意思 我们在一些sql存储过程,触发器等中经常会见到类似 N'xxxx' 是什么意思? 例如:if exists (select * from dbo.sysobje ...

  2. 类型“XXX”的控件“XXXX”必须放在具有 runat=server 的窗体标记内。

    问题:类型“Grid1”的控件“XXXX”必须放在具有 runat=server 的窗体标记内. 注:Grid1为表格的ID. 查找网上的解决大致为: 1)把Grid放到<form runat= ...

  3. 学习unity的第一个小游戏(Roll the ball)的笔记

    1.摄像机的跟随运动,逻辑就是保持摄像机跟主角的距离不变(Undate()函数). offset=trandform.position-player.position. Undate() { tran ...

  4. POJ 1743 - Musical Theme 最长不重叠重复子串

    题意:    给出一列数据,问你其中重复的最长连续子串的长度    但是有要求:        1. 长度至少为 5 .        2. 两串可以不相等,但两串每个对应位置的数字相减差值固定 (即 ...

  5. JS+css滑动菜单简单实现

    JS+css滑动菜单 制作一个简单的滑动菜单,当鼠标指向菜单标题时,滑出二级菜单.移开时二级菜单隐藏.目标很简单,实践时有一些细节需要注意,比如鼠标移向二级菜单的 过程中,二级菜单消失了.还有定位出错 ...

  6. Destoon标签使用技巧十则

    Destoon标签 1.全局标签 网站名称:{$DT[sitename]}网站地址:{DT_PATH}网站LOGO: {if $MODULE[$moduleid][logo]}{DT_SKIN}ima ...

  7. 使用div+iframe实现弹窗及弹出内容无法显示的解决

    使用div+iframe实现弹窗 除了使用实际的弹出窗口,还可以使用控制一个div的display属性来模拟一个弹出窗口的操作,这里使用在Div里放一个iFrame的方式,主要考虑到可以在需要的时候加 ...

  8. Windows文件居然有解锁一说,并且还会引起SignTool Error,真是昏倒!

    I'm running Windows 7 and when I try to run a batch file, it says, "The publisher could not be ...

  9. 读论文系列:Nearest Keyword Search in XML Documents中使用的数据结构(CT、ECT)

    Reference: [1]Y. Tao, S. Papadopoulos, C. Sheng, K. Stefanidis. Nearest Keyword Search in XML Docume ...

  10. windows中.msc文件详解

    msc是Microsoft Management Console的缩写.其实是一种可执行程序类型,可.exe类似.一般可以通过直接双击.msc文件或者在windows的运行中输入相应的文件名来启动. ...