委托是我们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几个常用手法的更多相关文章

  1. delegate Func Action Expression

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; na ...

  2. C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别

    以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...

  3. C# delegate event func action 匿名方法 lambda表达式

    delegate event action func 匿名方法 lambda表达式 delegate类似c++的函数指针,但是是类型安全的,可以指向多个函数, public delegate void ...

  4. 浅谈C#中常见的委托<Func,Action,Predicate>(转)

    一提到委托,浮现在我们脑海中的大概是听的最多的就是类似C++的函数指针吧,呵呵,至少我的第一个反应是这样的. 关于委托的定义和使用,已经有诸多的人讲解过,并且讲解细致入微,尤其是张子阳的那一篇.我就不 ...

  5. C#基础-Func,Action

    Func,Action 的介绍及其用法 Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如 ...

  6. [转载]C#基础-Func,Action

    Func,Action 的介绍及其用法 Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如 ...

  7. 系统内置委托:Func/Action

    lSystem.Func 代表有返回类型的委托 lpublic delegate TResult  Func<out TResult>(); lpublic delegate TResul ...

  8. C# Task中的Func, Action, Async与Await的使用

    在说Asnc和Await之前,先说明一下Func和Action委托, Task任务的基础的用法 1. Func Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate, ...

  9. C#的泛型委托Predicate/Func/Action

    Predicate<T> 是一个委托,它代表了一个方法,它的定义是: namespace System {    // 摘要:    表示定义一组条件并确定指定对象是否符合这些条件的方法. ...

随机推荐

  1. 查杀病毒的NB命令

    kill - `pgrep linux|awk '{++S[$1]}END{for (i in S) print i}'` teamkill #!/bin/bash set -x kill - `pg ...

  2. Maven 在eclipse中如何配置

    大部分码农们都很熟悉Eclipse,用eclipse开发项目,Maven是解决依赖库的一个非常好用的java工具,可以与Eclipse集成,方便地管理web,java项目等等:但是很多初学者都不知道怎 ...

  3. 窗体彻底关闭事件FormClosed

    //Application.Exit()是退出整个应用程序 Application.ExitThread();//强制中止调用线程上的所有消息,同样面临其它线程无法正确退出的问题 System.Dia ...

  4. angular学习笔记(二十)-表单验证

    本篇主要介绍angular中的表单验证: 表单验证主要有以下一些内容: 1. required指令: 相当于html5的required属性,验证不能为空 2. ng-maxlength属性: 验证内 ...

  5. angular学习笔记(十六) -- 过滤器(2)

    本篇主要介绍angular自定义的过滤器: 直接看例子: <!DOCTYPE html> <html ng-app="MyFilter"> <head ...

  6. 每日英语:Is Bo Xilai the Past or Future?

    Bo Xilai may be in jail, but a struggle is now underway within the Communist Party over the policies ...

  7. 随笔记:Python于Windows下初实践,及使用Connector/Python连接MySQL

    有一同事要离职了,我负责交接一个用Python同步数据的项目. 之前木有做过Python,周休,做个简单的查询数据库,小练一下手. 包含: 安装 连接.查询MySQL 列表 元组 for循环 whil ...

  8. 利用Python imaplib和email模块 读取邮件文本内容及附件内容

    python使用imap接收邮件的过程探索 https://www.cnblogs.com/yhlx/archive/2013/03/22/2975817.html #! encoding:utf8 ...

  9. Django 用ModelForm批量保存form表单(非常实用的方法) mfor_verity项目

    1.先看一下 Model 非常多 from django.db import models from users.models import UserProfile # Create your mod ...

  10. centos7搭建python3并和python2共存

    注意事项:1.非root帐号加上sudo2.centos7自带Python 2.7.5是不能卸载的,很多系统级软件依赖这个 安装依赖# yum -y groupinstall "Develo ...