委托学习续: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 ...
随机推荐
- Android:Resources资源文件
Android Resoureces是res目录下的那些目录和文件,常用的有: res/drawable/ 存放图片资源,类型有: 相关使用: Android:res之shape制作圆角 Androi ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- [ffmpeg 扩展第三方库编译系列] 关于libvpx mingw32编译问题
在编译libvpx的时候遇到挺多的问题, 1.[STRIP] libvpx.a < libvpx_g.a strip: Bad file number 这个错误也是比较难搞的,一开始以为只是 ...
- OM Price Lists
--select * --from org_organization_definitions; --execute fnd_client_info.set_org_context(111); -- ...
- Python各种模块下载及安装配置
方式1 在Python官网https://www.python.org/或者是github搜索进行下载 ,解压缩之后通过命令提示符进入已经解压缩文件夹根目录,输入下面的命令: python setup ...
- [原]Unity3D深入浅出 - 新版粒子系统 (Shuriken)
Shuriken粒子系统是继Unity3.5版本之后推出的新版粒子系统,它采用了模块化管理,个性化的粒子模块配合粒子曲线编辑器使用户更容易创作出各种兵分复杂的粒子效果. 创建一个粒子系统的方式有两种: ...
- ubuntu常用快捷键
1.直接退出窗口 Ctrl + q 2.窗口变大变小 Alt + Q 3.最小化窗口,显示桌面Ctrl + Alt + D 4.把当前窗口移到另一个工作区 Shift+ Ctrl + Alt +方 ...
- JPA---一对一关系
在JPA中,使用@oneToOne来标示. package com.yl.demo1.bean.oneToone; import javax.persistence.CascadeType; impo ...
- 【工具类】如何通过代码安装一个apk文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- HOG学习(一)
关于HOG里的特征维度 参考 http://gz-ricky.blogbus.com/logs/85326280.html http://blog.sina.com.cn/s/blog_7897fb6 ...