委托学习续:Action、Func和Predicate
我们先看一个上一章的委托的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test
{
class Program
{
static void Main(string[] args)
{
new Program(); Console.ReadKey();
} public delegate void AddDelegate(float a, int b); public Program()
{
AddDelegate add = addFunc; add(11.11f, );
} private void addFunc(float a, int b)
{
double c = a + b;
Console.WriteLine(c);
}
}
}
Action:
Action可以看做C#为我们提供的内置的没有返回值的委托,用在第一个例子里可以去掉委托的声明,代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test
{
class Program
{
static void Main(string[] args)
{
new Program(); Console.ReadKey();
} public Program()
{
//指定好参数数量和类型即可立即使用, 无需显示的定义委托
Action<float, int> add = addFunc; add(11.11f, );
} private void addFunc(float a, int b)
{
double c = a + b;
Console.WriteLine(c);
}
}
}
同时,匿名函数和Lambda的写法也是允许的,详情可以查看上一篇笔记。
Func:
类似Action,Func是C#为我们提供的内置的具有返回值的委托,而返回值的类型为最后一个被指定的类型,请看下面的例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test
{
class Program
{
static void Main(string[] args)
{
new Program(); Console.ReadKey();
} public Program()
{
//指定好参数数量和类型即可立即使用, 无需显示的定义委托
//对于 Func 委托来说, 最后指定的类型为返回的类型
Func<float, int, string> add = addFunc; string result = add(11.11f, );
Console.WriteLine(result);
} private string addFunc(float a, int b)
{
double c = a + b;
Console.WriteLine(c);
return "hello: " + c.ToString();
}
}
}
Predicate:
该委托专门用于过滤集合中的元素,下面我们看一个例子,保留数组中的正整数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test
{
class Program
{
static void Main(string[] args)
{
new Program(); Console.ReadKey();
} public Program()
{
//指定的类型为需要过滤的数组的元素类型
Predicate<int> filter;
filter = IntFilter; int[] nums = new int[]{, -, -, , -, , , -};
//开始进行过滤, 返回 true 表示留下当前元素
int[] result = Array.FindAll(nums, filter); for(int i = ; i < result.Length; i++)
{
Console.WriteLine(result[i]);
}
} private bool IntFilter(int i)
{
if (i >= )
{
return true;
}
return false;
}
}
}
委托学习续:Action、Func和Predicate的更多相关文章
- c# Action,Func,Predicate委托
System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...
- (C#) Action, Func, Predicate 等泛型委托
(转载网络文章) (1). delegate delegate我们常用到的一种声明 Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型. 例:public del ...
- C#常见委托のdelegate定义,Func,Action,Predicate总结
委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板:(交流可以加qq群:435226676) 委托常见的方式有一般委托显示定义,Func<T,TResult> (T, ...
- 委托delegate,Action,Func,Predicate
C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...
- Delegate,Action,Func,Predicate的使用与区别
C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...
- C#中匿名函数、委托delegate和Action、Func、Expression、还有Lambda的关系和区别
以前一直迷迷糊糊的,现在总算搞明白. Lambda表达式 Lamda表达式基本写法是()=>{ };Lambda和方法一样都可以传入参数和拥有返回值.(int x)=>{return x; ...
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...
- VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
[提示] 1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的 Action与Func委托.2.如果您对单 ...
- Lambda Action Func练习
namespace lambda { delegate void TestDelegate(string s); class Program { static void Main(string[] a ...
随机推荐
- Node.js学习(11)----HTTP服务器与客户端
Node.js 标准库提供了 http 模块,其中封装了一个高效的 HTTP 服务器和一个简易的HTTP 客户端.http.Server 是一个基于事件的 HTTP 服务器,它的核心由 Node.js ...
- caffe简易上手指南(三)—— 使用模型进行fine tune
之前的教程我们说了如何使用caffe训练自己的模型,下面我们来说一下如何fine tune. 所谓fine tune就是用别人训练好的模型,加上我们自己的数据,来训练新的模型.fine tune相当于 ...
- 摄像头(4)用Camera和SurfaceView自定义拍照程序
定制拍照程序的基本步骤 1,打开照相机:Camera.open 这是独占方式打开的 2,创建SurfaceView对象 多缓冲,多线程view 3,添加回调事件监听器(SurfaceHolder.ad ...
- Maximum Allowed Error 7 错误解决
http://blog.csdn.net/lyx123/article/details/6238167 这段时间,一直在做WINCE 的应用,后来将NK做大后,必须修改EBOOT,以便能够提供较大的空 ...
- Managed C++中使用Nullable<T>
非null值表示和C#的用法一样. Nullable<int> a = 1; null值的表示: Nullable<int> a = Nullable<int>() ...
- Zookeeper命令
常用命令 ZooKeeper 支持某些特定的四字命令字母与其的交互.它们大多是查询命令,用来获取 ZooKeeper 服务的当前状态及相关信息.用户在客户端可以通过 telnet 或 nc 向 Zoo ...
- poj 1201 Intervals(差分约束)
题目:http://poj.org/problem?id=1201 题意:给定n组数据,每组有ai,bi,ci,要求在区间[ai,bi]内至少找ci个数, 并使得找的数字组成的数组Z的长度最小. #i ...
- poj 1039 Pipe(叉乘。。。)
题目:http://poj.org/problem?id=1039 题意:有一宽度为1的折线管道,上面顶点为(xi,yi),所对应的下面顶点为(xi,yi-1),假设管道都是不透明的,不反射的,光线从 ...
- 浏览器兼容问题系列---使IE支持CSS3 Media Quary
兼容是一件很让前端攻城师头疼的事情,笔者今天在做一个Demo的时候就碰到了一个问题(大牛就不要拍砖了,谢谢!) 经常做移动互联网前端的攻城师想必对于css3 media quary已经很熟悉了,但是碰 ...
- 如何使用UDP进行跨网段广播
广播域首先我们来了解一下广播域的概念.广播域是网络中能接收任一台主机发出的广播帧的所有主机集合.也就是说,如果广播域内的其中一台主机发出一个广播帧,同一广播域内所有的其它主机都可以收到该广播帧.广播域 ...