What is Func<T,TResult> in C#?

In simple terms,Func<T,TResult> is just generic delegate. Depending on the requirement,the type parameters(T and TResult) can be replaced with the corresponding(对应的) type arguments.

For example,Func<Employee,string> is a delegate that represents(代表) a function expecting(期待) Employee object as an input parameter and returns a string.

class Program
{
static void Main(string[] args)
{
List<Employee> employees = new List<Employee>() {
new Employee{ID=,Name="lin1"},
new Employee{ID=,Name="lin2"},
new Employee{ID=,Name="lin3"}
}; //Func<Employee, string> selector = e => "name=" + e.Name;
IEnumerable<string> employeeNames = employees.Select(e => "name=" + e.Name);
foreach (string name in employeeNames)
{
Console.WriteLine(name);
} }
}
class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}

What is the difference between Func delegate and lambda expression?

They're the same, just two different ways to write the same thing. The lambda syntax is newer, more concise(简洁) and easy to write.

What if I have to pass two or more input parameters?

As of this recording there are 17 overloaded versions of Func, which enables us to pass variable number and type of input parameters. In the example below, Func<int,int,string> represents a function that expects 2 int input parameters and returns a string.

class Program
{
static void Main(string[] args)
{
Func<int, int, string> funcDelegate = (num1, num2) => (num1 + num2).ToString();
string result = funcDelegate(10,20);
Console.WriteLine("sum="+result); }
}

Finally, I completely learning the  C# language, go to bed, good night guy.

Part 100 Func delegate in c#的更多相关文章

  1. Expression<Func<TObject, bool>>与Func<TObject, bool>的区别

    Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后 ...

  2. [C#] 委托之Action和Func区别

    一.说明 一般我们定义委托都是有如下两步: public delegate void MyDelegate(string name);//定义委托 public MyDelegate myDelega ...

  3. lambda表达式Expression<Func<Person, bool>> 、Func<Person, bool>区别

    前言: 自己通过lambda表达式的封装,将对应的表达式转成字符串的过程中,对lambda表达式有了新的认识 原因: 很多开发者对lambda表达式Expression<Func<Pers ...

  4. Expression<Func<T, bool>>与Func<T, bool>的区别

    转自:http://www.cnblogs.com/wow-xc/articles/4952233.html Func<TObject, bool>是委托(delegate) Expres ...

  5. EF Core 封装方法Expression<Func<TObject, bool>>与Func<TObject, bool>区别

    unc<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后就 ...

  6. 浅谈C#中常见的委托<Func,Action,Predicate>(转)

    一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...

  7. action func用法记记

    public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void s ...

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

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

  9. Delegate & Event

    Long time without coding,貌似对programming都失去了曾有的一点点sense了,今日有空再细瞄一下.net的委托和事件. Delegate 首先,委托用于引用一类具有相 ...

随机推荐

  1. 【JavsScript】一个元素绑定多个事件

    <script> document.getElementById("a").addEventListener("click", function() ...

  2. Android端百度地图API使用详解

    百度地图API简介 百度地图移动版API(Android)是一套基于Android设备的应用程序接口,通过该接口,可以轻松的访问百度服务和数据,构建功能丰富.交互性强的地图应用程序. 百度地图移动版A ...

  3. purge

    RR级别 mysql V5.6 debug ; 测试1 会话1: mysql)); Query OK, rows affected (0.04 sec) mysql,"a"); Q ...

  4. fs/ext2/inode.c相关函数注释

    用数组chain[4]描述四种不同的索引,即直接索引.一级间接索引.二级间接索引.三级间接索引.举例说明这个结构各个域的含义.如果文件内的块号为8,则不需要间接索引,所以只用chain[0]一个Ind ...

  5. 基于jQuery的网站首页宽屏焦点图幻灯片

    今天给大家分享一款基于jQuery的网站首页宽屏焦点图幻灯片.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览 ...

  6. Asp.Net处理URL空格变%20问题

    在Web前端需要页面跳转的时候我们可能会这样子用:window.location.href = "page.html?parameters",如果刚好parameters里面带有空 ...

  7. Centos7安装杀毒软件ClamAV

    Clam AntiVirus(ClamAV)是免费而且开放源代码的防毒软件,软件与病毒码的更新皆由社群免费发布.目前ClamAV主要是使用在Linux.FreeBSD等Unix-like系统架设的邮件 ...

  8. 关于c#调用c/c++ dll遇到的问题总结

    前段时间公司做了个winform程序,需要调用c 的dll去读取卡号的程序,期间遇到些问题,下面来分享下 一.dll路径问题 相信很多开发者都会遇到这个问题,我总结了下我现在有3总方式去解决这个问题: ...

  9. 11. Android框架和工具之 Logger(调试代码)

    1. Logger Logger是android是一个简单.漂亮.功能强大的Android日志程序. 日志程序提供了 : 线程信息Thread information 类信息Class informa ...

  10. 控制Input框输入的为大写字母

    本来我的想法是Jquery来控制的,万万没想到...用Css就可以实现!!!! .toUp input{    text-transform:uppercase; }     感谢:  http:// ...