参考代码1:

using System;
using System.Collections.Generic;
using System.Linq;

namespace FuncActionDemo
{
    class FuncActionTest
    {
        public void Test1(List<int> data, Func<int, bool> filter)
        {
            var result = data.Where(x => filter(x)).ToList();
            result.ForEach(x =>
            {
                Console.WriteLine(x);
            }
            );

        }
        public void Test2(List<int> data, Action<int, int> action)
        {
            var d = data.OrderBy(x => x).ToList();
            action(d[0], d[1]);

        }
        public void Test3(List<int> data, Func<int, int,int> func)
        {
            var d = data.Where(x => x%2==1).ToList();
            int iOddAccSum=  d.Aggregate(func);
            Console.WriteLine("Odd Acc Sum:"+iOddAccSum.ToString());

        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            FuncActionTest faTest = new FuncActionTest();
            List<int> data = new List<int> { 1, 2, 4, 7, 8, 9 };
            faTest.Test1(data, x => {
                if (x % 2 == 0)
                    return true;
                else
                    return false;
            });
            faTest.Test1(data, x => { return FilterEven(x); });
            faTest.Test1(data, FilterEven);
            Console.WriteLine("-----------");
            faTest.Test2(data, Add);
            faTest.Test3(data,Acc);
        }
        static bool FilterEven(int s)
        {
            if (s % 2 == 0)
                return true;
            else
                return false;
        }
        static void Add(int a, int b)
        {
            Console.WriteLine(a + b);
        }

        static int Acc(int a, int b)
        {
            return a * 10 + b;
        }
    }
}

参考代码2:

using System;
using System.Linq; namespace AggregateDemo
{
class Program
{
static void Main(string[] args)
{
string[] words = new string[] { "My", "Name", "Is", "Zhang San" };
string s = words.Aggregate((a, n) => a + " " + n);
Console.WriteLine(s); int[] ints = new int[] { 1,2,3,4,5,6};
int iSum = ints.Aggregate(1000,(a, b) => a*10+b);
Console.WriteLine(iSum);
}
}
}

参考代码3:

[C#] Func及Action的快速Demo的更多相关文章

  1. 通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系

    先说一下个人理解的结论吧: delegate是C#中的一种类型,它实际上是一个能够持有对某个方法的引用的类. delegate声明的变量与delegate声明的事件,并没有本质的区别,事件是在dele ...

  2. Func和Action委托简单用法

    Func和Action类是特殊的类型,它们允许你在不必指定自定义委托类型的情况下,去使用委托.在整个.NET框架中都可以使用它们.例如,在我们考察并行计算时,你也会看到这两个类的示例. 上面一段文字是 ...

  3. Func,Action 的介绍

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

  4. c# 委托(Func、Action)

    以前自己写委托都用 delegate, 最近看组里的大佬们都用 Func , 以及 Action 来实现, 代码简洁了不少, 但是看得我晕晕乎乎. 花点时间研究一下,记录一下,以便后期的查阅. 1.F ...

  5. 第十节:委托和事件(2)(泛型委托、Func和Action、事件及与委托的比较)

    一. 泛型委托 所谓的泛型委托,即自定义委托的参数可以用泛型约束,同时内置委托Func和Action本身就是泛型委托. 将上一个章节中的Calculator类中的方法用自定义泛型委托重新实现一下. p ...

  6. C# Func与Action

    Func与Action是C#的内置委托,在使用委托时,可不必再定义. (1)Func:有返回类型的委托. Func类型的委托,肯定有一个返回类型,如果Func只有一个参数,那么它就是代表没有参数但是有 ...

  7. .NET自带泛型委托方法Func、Action和Predicate

    Func.Action和Predicate是.NET自带的3个泛型委托方法,三个方法的区别其实并不大,要强行给混着用也是可以的,但是我们是有追求的人,把道理讲清楚总是好的. 一.Func是有返回值的方 ...

  8. Func 和 Action 委托

    有了泛型委托,就有了一能适用于任何返回类型和任意参数(类型和合理的个数)的通用委托,Func 和 Action.如下所示(下面的in表示参数,out表示返回结果): delegate TResult ...

  9. 回炉重造系列-C# func and action委托是什么?

    如题: C# func and action委托是什么? 1) 回答这个问题之前,我们需要了解什么是委托(英文 Delegate )? 为了便于理解,再往前推一步,回到c语言时代,指针的概念. 什么是 ...

  10. Linq 入门 顺带 Func与Action

    Linq的优点: 查询是一种从数据源检索数据的表达式. 查询通常用专门的查询语言来表示. 随着时间的推移,人们已经为各种数据源开发了不同的语言:例如,用于关系数据库的 SQL 和用于 XML 的 XQ ...

随机推荐

  1. sublime json格式化

    1. 插件安装 install package-->pretty json 2. 快捷键设置 设置(preference):热键设置 [ {"keys": ["al ...

  2. mybatis-关联查询3-自关联查询

    一对多的方式处理 查询指定栏目的所有子孙栏目 查询指定栏目及其所有子孙栏目 多对 一的方式处理

  3. SVN安装配置手册

    1.官网下载相关的服务端的安装包 SVN(版本控制器): SVN-客户端:TortoiseSVN SVN-服务端:VisualSVN 下载地址: TortoiseSVN:https://tortois ...

  4. Vuex4.x 简介及state、getters、mutations、actions详解(OptionApi 和 CompositionApi)

    http://t.zoukankan.com/yaopengfei-p-15439203.html 作       者 : Yaopengfei(姚鹏飞) 博客地址 : http://www.cnbl ...

  5. ORACLE监听无法启动的几个原因

    1./etc/hosts中配置问题 误删了127.0.0.1的默认记录 2./var/tmp/.oracle的权限问题 TNS-12546:TNS:permission denied TNS-1256 ...

  6. vue的表单

    你可以用 v-model 指令在表单控件元素上创建双向数据绑定. 输入框 实例中演示了 input 和 textarea 元素中使用 v-model 实现双向数据绑定: <!DOCTYPE ht ...

  7. iOS开发之运行报错 dyld: Library not loaded: *** Reason: image not found

    xcode运行报错 dyld: Library not loaded: @rpath/Flutter.framework/Flutter   Referenced from: /private/var ...

  8. omcat启动Publishing failed with multiple errors

    新安装一个tomcat插件.启动的时候就弹错误框.但tomcat还能使用. Publishingfailedwithmultipleerrors Resource is out of sync wit ...

  9. SQL-分组聚合

    -- 语法 select * |列名|表达式         -- 5 from 表名                         -- 1 where 条件                    ...

  10. vue + antV G6 实现流程图完整代码 (antv G6 流程图)

    效果如下: 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...