[C#] Func及Action的快速Demo
参考代码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的更多相关文章
- 通过IL分析C#中的委托、事件、Func、Action、Predicate之间的区别与联系
先说一下个人理解的结论吧: delegate是C#中的一种类型,它实际上是一个能够持有对某个方法的引用的类. delegate声明的变量与delegate声明的事件,并没有本质的区别,事件是在dele ...
- Func和Action委托简单用法
Func和Action类是特殊的类型,它们允许你在不必指定自定义委托类型的情况下,去使用委托.在整个.NET框架中都可以使用它们.例如,在我们考察并行计算时,你也会看到这两个类的示例. 上面一段文字是 ...
- Func,Action 的介绍
Func,Action 的介绍 Func是一种委托,这是在3.5里面新增的,2.0里面我们使用委托是用Delegate,Func位于System.Core命名空间下,使用委托可以提升效率,例如在反射中 ...
- c# 委托(Func、Action)
以前自己写委托都用 delegate, 最近看组里的大佬们都用 Func , 以及 Action 来实现, 代码简洁了不少, 但是看得我晕晕乎乎. 花点时间研究一下,记录一下,以便后期的查阅. 1.F ...
- 第十节:委托和事件(2)(泛型委托、Func和Action、事件及与委托的比较)
一. 泛型委托 所谓的泛型委托,即自定义委托的参数可以用泛型约束,同时内置委托Func和Action本身就是泛型委托. 将上一个章节中的Calculator类中的方法用自定义泛型委托重新实现一下. p ...
- C# Func与Action
Func与Action是C#的内置委托,在使用委托时,可不必再定义. (1)Func:有返回类型的委托. Func类型的委托,肯定有一个返回类型,如果Func只有一个参数,那么它就是代表没有参数但是有 ...
- .NET自带泛型委托方法Func、Action和Predicate
Func.Action和Predicate是.NET自带的3个泛型委托方法,三个方法的区别其实并不大,要强行给混着用也是可以的,但是我们是有追求的人,把道理讲清楚总是好的. 一.Func是有返回值的方 ...
- Func 和 Action 委托
有了泛型委托,就有了一能适用于任何返回类型和任意参数(类型和合理的个数)的通用委托,Func 和 Action.如下所示(下面的in表示参数,out表示返回结果): delegate TResult ...
- 回炉重造系列-C# func and action委托是什么?
如题: C# func and action委托是什么? 1) 回答这个问题之前,我们需要了解什么是委托(英文 Delegate )? 为了便于理解,再往前推一步,回到c语言时代,指针的概念. 什么是 ...
- Linq 入门 顺带 Func与Action
Linq的优点: 查询是一种从数据源检索数据的表达式. 查询通常用专门的查询语言来表示. 随着时间的推移,人们已经为各种数据源开发了不同的语言:例如,用于关系数据库的 SQL 和用于 XML 的 XQ ...
随机推荐
- Markdown 基础语法 备忘录
在vscode中使用Markdown先要安装一些插件: Markdown Preview Enhanced Paste Image LimfxCodeEX Code Spell Checker 一级标 ...
- 栈和寄存器虚拟机比较(以python和lua为例)
指令长度 python python的指令定长,长度为16bit,其中8bit操作码,8bit操作数. ///@file: Python-3.6.0\Include\code.h typedef ui ...
- sos 扩展命令文档
https://learn.microsoft.com/zh-cn/dotnet/framework/tools/sos-dll-sos-debugging-extension?redirectedf ...
- HTTP请求报文(请求行,请求头,请求体)
HTTP协议 1.简介 HTTP协议(Hyper Text Transfer Protocol,超文本传输协议),是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...
- leetcode 27. 移除元素 【时间击败100.00%】【内存击败84.67%】
1 public int removeElement(int[] nums, int val) { 2 int last = nums.length - 1; 3 for (int i = 0; i ...
- java自定义的异常类
java自定义的异常类 1.自定义异常类,需要继承 RuntimeException @Datapublic class EmployeeCheckException extends RuntimeE ...
- Python标准库模块之heapq
创建堆 heapq有两种方式创建堆, 一种是使用一个空列表,然后使用heapq.heappush()函数把值加入堆中,另外一种就是使用heap.heapify(list)转换列表成为堆结构 #创建堆方 ...
- mysql使用保留字导致该列查不出来(mysql版本问题)
mysql版本是 问题: 如图这边groups是sql的保留字此时这样查询是查不出来的,并会报错语法错误.但是在5.几的mysql版本中这行sql就没有问题. 解决方法: 在groups列上加上'gr ...
- pandas的数据结构--Series创建使用
# 1. 使用Series创建一个空的系列:import pandas as pds=pd.Series()print(s)输出结果为:Series([], dtype: float64) # 2. ...
- 【BOOK】正则表达式
正则表达式 1. 开源中国-正则表达式测试工具:https://tool.oschina.net/regex/ 2. 匹配规则 3. match() 从字符串起始位置匹配正则表达式 若从起始位置匹配不 ...