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.

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. List<Employee> employees = new List<Employee>() {
  6. new Employee{ID=,Name="lin1"},
  7. new Employee{ID=,Name="lin2"},
  8. new Employee{ID=,Name="lin3"}
  9. };
  10.  
  11. //Func<Employee, string> selector = e => "name=" + e.Name;
  12. IEnumerable<string> employeeNames = employees.Select(e => "name=" + e.Name);
  13. foreach (string name in employeeNames)
  14. {
  15. Console.WriteLine(name);
  16. }
  17.  
  18. }
  19. }
  20. class Employee
  21. {
  22. public int ID { get; set; }
  23. public string Name { get; set; }
  24. }

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.

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Func<int, int, string> funcDelegate = (num1, num2) => (num1 + num2).ToString();
  6. string result = funcDelegate(10,20);
  7. Console.WriteLine("sum="+result);
  8.  
  9. }
  10. }

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. VCL -- Understanding the Message-Handling System

    Understanding the Message-Handling System http://docwiki.embarcadero.com/RADStudio/XE7/en/Understand ...

  2. [转]iOS应用性能调优的25个建议和技巧

    写在前面 本文来自iOS Tutorial Team 的 Marcelo Fabri,他是Movile的一名 iOS 程序员.这是他的个人网站:http://www.marcelofabri.com/ ...

  3. [Express] Level 2: Middleware -- 1

    Mounting Middleware Given an application instance is set to the app variable, which of the following ...

  4. Cocos2d-x v3.0 新的事件调度方法 lambda表达式的使用

    欢迎添� Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/24603251 Coc ...

  5. C# unix时间戳转换

    场景:由于业务需要和java 开发的xxx系统对接日志,xxx系统中用“1465195479100” 来表示时间,C# 里面需要转换做一下逻辑处理,见代码段. C#获取的unix时间戳是10位,原因是 ...

  6. Traveling by Stagecoach 状态压缩裸题

    Traveling by Stagecoach dp[s][v]  从源点到达  v,状态为s,v的最小值.  for循环枚举就行了. #include <iostream> #inclu ...

  7. Maximum repetition substring 后缀数组

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7578   Acc ...

  8. matlab 和 origin作图去除白边,字体调节

    一 matlab作图 (1)在图形文件figure的菜单上点击file->export setup, 在size选项中,对"expand axes to fill figure&quo ...

  9. Java、Android中Math详解

    java.math.Math类常用的常量和方法: Math.PI 记录的圆周率 Math.E记录e的常量 Math.abs 求绝对值 Math.sin 正弦函数 Math.asin 反正弦函数 Mat ...

  10. iOS UILable的一些用法

    1.按钮上的文字添加下划线 问题:实现下图中右侧的按钮文字效果 代码(绿色为按钮文字加下划线方法): [MyTools createMyImageview:topEditView frame:CGRe ...