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 首先,委托用于引用一类具有相 ...
随机推荐
- 番茄钟App(Pomodoro Tracker)
最近为了学习Swift编程语言,写了一个番茄钟的App(Pomodoro Tracker).刚上线的1.2版本增加了Apple Watch的支持. iPhone版 Apple Watch版 如果你跟我 ...
- Delphi Data Type to C# Data Type
Delphi DataType C# datatype ansistring string boolean bool byte byte char char comp double currency ...
- uoj #5. 【NOI2014】动物园 kmp
#5. [NOI2014]动物园 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/5 Description 近日 ...
- 日志记录到txt文件
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;usi ...
- iis7.5配置.net mvc注意事项
iis7.5配置.net mvc注意事项 1. 应用程序池采用经典模式,framework4.0.可能存在权限问题,解决办法:在高级设置的标识设为LocalSystem.一般mvc都采用集成模式, ...
- iOS开发——UI篇Swift篇&UIImageView
UIImageView override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString //通过坐标和大 ...
- memached+asp.net 4.0 分布式缓存
由于准备做一个商品站点,希望做一个memached缓存.折腾了一个多星期.本机是存进去取出来为空. 各种办法都试过了,还是不行.最后用同事电脑測试是能够的,然后将DEMO公布到阿里云也是能够的.支持. ...
- Perl 内部结构详解
PerlGuts Illustrated Version 0.49, for perl 5.20 and older This document is meant to supplement the ...
- Cache的原理、设计及实现
Cache的原理.设计及实现 前言 虽然CPU主频的提升会带动系统性能的改善,但系统性能的提高不仅仅取决于CPU,还与系统架构.指令结构.信息在各个部件之间的传送速度及存储部件的存取速度等因素有关,特 ...
- [WinForm] 使用反射将业务对象绑定到窗体或控件容器
在WebForm中,可以使用反射将业务对象绑定到 ASP.NET 窗体控件.最近做Winform项目,也参考WebForm中的代码实现同样的功能. Winform没有提供类似WebForm中的 ...