Part 100 Func delegate in c#
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#的更多相关文章
- Expression<Func<TObject, bool>>与Func<TObject, bool>的区别
Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后 ...
- [C#] 委托之Action和Func区别
一.说明 一般我们定义委托都是有如下两步: public delegate void MyDelegate(string name);//定义委托 public MyDelegate myDelega ...
- lambda表达式Expression<Func<Person, bool>> 、Func<Person, bool>区别
前言: 自己通过lambda表达式的封装,将对应的表达式转成字符串的过程中,对lambda表达式有了新的认识 原因: 很多开发者对lambda表达式Expression<Func<Pers ...
- Expression<Func<T, bool>>与Func<T, bool>的区别
转自:http://www.cnblogs.com/wow-xc/articles/4952233.html Func<TObject, bool>是委托(delegate) Expres ...
- EF Core 封装方法Expression<Func<TObject, bool>>与Func<TObject, bool>区别
unc<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后就 ...
- 浅谈C#中常见的委托<Func,Action,Predicate>(转)
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
- action func用法记记
public partial class Form1 : Form { public Form1() { InitializeComponent(); } public delegate void s ...
- C#中常见的委托(Func委托、Action委托、Predicate委托)
今天我要说的是C#中的三种委托方式:Func委托,Action委托,Predicate委托以及这三种委托的常见使用场景. Func,Action,Predicate全面解析 首先来说明Func委托,通 ...
- Delegate & Event
Long time without coding,貌似对programming都失去了曾有的一点点sense了,今日有空再细瞄一下.net的委托和事件. Delegate 首先,委托用于引用一类具有相 ...
随机推荐
- Programming pages of Jasper Neumann
http://programming.sirrida.de/ Discussion topics Bit permutations Download source files List of func ...
- 线性回归(linear regression)之监督学习
假设有以下面积和房屋价格的数据集: 可以在坐标中画出数据的情况: 就是基于这样一个数据集,假定给出一个房屋的面积,如何预测出它的价格?很显然就是我们只需建立一个关于房屋面积的函数,输出就是房屋的价格. ...
- 【M23】考虑使用其他程序库
1.程序库的设计是一种权衡的结果.体积小,速度快往往不能移植.可移植,通用的程序库往往意味着冗余和效率低下. 2.因此,选择程序库的时候,需要进行取舍.比如:iostream和stdio.iostre ...
- hdu 4859 海岸线 最小割
海岸线 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4859 Description 欢迎来到珠海! 由于土地资源越来越紧张,使得许多海滨城市都只能 ...
- 【转】Delphi多线程学习(9):多线程数据库查询(ADO)
原文:http://www.cnblogs.com/djcsch2001/articles/2382559.html ADO多线程数据库查询通常会出现3个问题: 1.CoInitialize 没有调用 ...
- vs2013 设置为中文版
- [ES6] 15. Generators -- 2
Using for..of statement: function* greeting(){ console.log(`Generators are "lazy"`); yield ...
- Session丢失,都是CDN惹的祸
周六下午,正在外面吃饭,运营的同事火急火燎地给我打电话,说是网站出问题了,用户登录不了,而且这种情况也不是全部,只有部分地区有问题.没办法,只能回到家里找问题,打开代码,翻来覆去地找问题,搞了整整一下 ...
- dpkg的用法
转载:http://blog.csdn.net/sunjiajiang/article/details/7252593 dpkg是一个Debian的一个命令行工具,它可以用来安装.删除.构建和管理De ...
- 多线程和并发管理 .NET多线程服务
线程相关静态变量 默认静态变量应用程序域所有线程可见.如果静态变量需要在线程间共享,同步访问也就必然了. 线程相关静态变量保证线程安全,同一时间只有一个线程可访问,且每个线程都有该静态变量的拷贝. p ...