delegate、Func、Action几个常用手法
委托是我们C#开发当中使用频率非常的高一个手段,好处我就不列举了。
委托早期版本中只有delegate,后期版本有了Func(有返回值)和Action(无返回值)两委托方法。
我这里将列举它们三个常用的表现方式,还是老规矩上代码:
class Program
{
private delegate int GetSum(List<Product> list); // First Definition Delegate
static void Main(string[] args)
{
//C# delegate List<Product> list = new List<Product>()
{
new Product{ProductName="Iphone4s", Price=},
new Product{ProductName="Iphone5",Price=},
new Product{ProductName="Ipad4",Price=}
};
GetSum s = GetTotal;
Console.WriteLine(s(list)); //C# Func //Method A
Func<List<Product>, int> func = GetTotal;
Console.WriteLine(func(list)); //Method B
Func<List<Product>, int> func1 = delegate(List<Product> listA)
{ return listA.Sum(p => p.Price);
};
Console.WriteLine(func1(list)); //Method C
Func<List<Product>,int> func2=listB=>{ return listB.Sum(p => p.Price);
};
Console.WriteLine(func2(list)); //C# Action Action<List<Product>> action = listC =>
{
listC.ForEach(p => Console.WriteLine(p.Price));
}; action(list);
Console.Read(); } public static int GetTotal(List<Product> list)
{
return list.Select(p => p.Price).Sum();
}
} public class Product
{
public string ProductName{get;set;}
public int Price{get;set;}
}
delegate、Func、Action几个常用手法的更多相关文章
- delegate Func Action Expression
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; na ...
- C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别
以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...
- C# delegate event func action 匿名方法 lambda表达式
delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...
- 浅谈C#中常见的委托<Func,Action,Predicate>(转)
一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...
- C#基础-Func,Action
Func,Action 的介绍及其用法 Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如 ...
- [转载]C#基础-Func,Action
Func,Action 的介绍及其用法 Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如 ...
- 系统内置委托:Func/Action
lSystem.Func 代表有返回类型的委托 lpublic delegate TResult Func<out TResult>(); lpublic delegate TResul ...
- C# Task中的Func, Action, Async与Await的使用
在说Asnc和Await之前,先说明一下Func和Action委托, Task任务的基础的用法 1. Func Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate, ...
- C#的泛型委托Predicate/Func/Action
Predicate<T> 是一个委托,它代表了一个方法,它的定义是: namespace System { // 摘要: 表示定义一组条件并确定指定对象是否符合这些条件的方法. ...
随机推荐
- maven(2)------maven构建项目
一 下载maven 官网地址: http://maven.apache.org/download.cgi 如图: 可以下载历史版本. 二 windows下maven配置 1. 解压下载后的包,解压后 ...
- 每日英语:Tech Firms Flock to Vietnam
Opening up a Korean restaurant among the rice fields and limestone karsts north of Hanoi might seem ...
- form的method用get导致中文乱码
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- LeetCode: Word Break II 解题报告
Word Break II Given a string s and a dictionary of words dict, add spaces in s to construct a senten ...
- response.setContentType 与response.setCharacterEncoding
response.setContentType 设置发送到客户端的响应的内容类型,可以包括字符编码说明. 也就是说在服务器端坐了这个设置,那么他将在浏览器端起到作用,在你打开浏览器时决定编码方式 ...
- IE11不支持Selenium 2.0的解决方法
题前话(Pre-words) 希望使用Selenium 2.0的人看到这篇文章能够收藏此文,以后遇到该问题,再也不用花费多余的时间进行research了!本文就是对网上所有千奇百怪各种各样的searc ...
- tomcat架构分析 (connector NIO 实现)
出处:http://gearever.iteye.com 上一篇简单记录了缺省配置的connector的内部构造及消息流,同时此connector也是基于BIO的实现.除了BIO外,也可以通过配置快速 ...
- 来自阿里的 json 解析方案 fastjson
说起Json 解析,有非常多方法,不管是出自Google 的Gson也好,还是来自其它的某某.想必大家都非常熟悉. 今日在github上闲逛.偶遇 一 json 解析库.看起来非常不错,据说是眼下最快 ...
- js获取checkbox值的方法
js获取checkbox值的方法.分享给大家供大家参考.具体实现方法如下:<html> <head> <meta http-equiv="Content-Typ ...
- 【C#/WPF】用System.Timers.Timer计时器做浮窗广告
需求:鼠标静止一段时间后,显示浮窗广告. 思路:界面XAML写好一个专门显示浮窗广告的Canvas,先设为不可见Visibility=”Collapsed”,然后用System.Timers.Time ...