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. C++ Bit Fields

    http://msdn.microsoft.com/en-us/library/ewwyfdbe%28v=vs.71%29.aspx Note An unnamed bit field of widt ...

  2. MON166 FAQ

    MON166: SOFTWARE RESET USING THE MONITOR QUESTION What happens when debugging using MON166 and my pr ...

  3. windows的iis做后门,隐藏访问,无日志<转>

    windows下的iis5/iis6做后门,隐藏访问,不留访问记录或者不留日志 好不容易攻下一台Windows2000/2003 IIS服务器,你一定会想,怎样才能长期占有这个“肉鸡”呢?聪明的你肯定 ...

  4. Swift学习笔记十二

    方法 方法就是和某种特定类型相关联的函数.类.结构体.枚举都可以定义实例方法和类型方法.类型方法和OC中的类方法类似. 结构体和枚举也可以定义方法是Swift与C/OC之间很大的一个区别,在OC中,只 ...

  5. jquery实现页面局部刷新

    后台管理中总是使用frameset进行分成部分进行管理,但是感觉很不好用,尤其是页面间调转还要判断window.parent,太令我费神了,于是学习使用XMLHttpRequest进行页面局部刷新.代 ...

  6. oracle 迁移到 mysql(结构和数据)

    1下载MySQL Migration Toolkit 2安装:jdk-6u38-ea-bin-b04-windows-amd64-31_oct_2012.exe 3下载ojdbc14.jar 具体地址 ...

  7. ios开发——实用技术总结Swift篇&swift常用开发技术总结

    swift常用开发技术总结 懒加载:属性,数组(字典),控件... 数组(懒加载): lazy var shops:Array<Dictionary<String, String>& ...

  8. Linux中断(interrupt)子系统之一:中断系统基本原理 (图解)

    http://blog.csdn.net/droidphone/article/details/7445825

  9. navigationController pushViewController 多次跳转后怎么返回

    使用NavigationViewController进行页面跳转时,应该使用pushViewController方法来跳转至下一页面,这样的话,下一页面同样在NavigationViewControl ...

  10. oracle的触发器

    oracle的触发器分为语句级和行级两种类型,在视图上所创建的触发器叫做什么类型的触发器? DML触发器有三类: 1, insert触发器: 2, update触发器: 3, delete触发器: 触 ...