我们先看一个上一章的委托的例子:

 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的更多相关文章

  1. c# Action,Func,Predicate委托

    System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...

  2. (C#) Action, Func, Predicate 等泛型委托

    (转载网络文章) (1). delegate delegate我们常用到的一种声明   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.   例:public del ...

  3. C#常见委托のdelegate定义,Func,Action,Predicate总结

    委托,顾名思义,就是让其他代理,本质就是为具有共性方法组定义一个方法模板:(交流可以加qq群:435226676) 委托常见的方式有一般委托显示定义,Func<T,TResult> (T, ...

  4. 委托delegate,Action,Func,Predicate

    C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...

  5. Delegate,Action,Func,Predicate的使用与区别

    C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...

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

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

  7. Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)

    Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...

  8. VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试

    [提示] 1. 阅读文本前希望您具备如下知识:了解单元测试,了解Dynamic,熟悉泛型(协变与逆变)和Lambda,熟悉.NET Framework提供的 Action与Func委托.2.如果您对单 ...

  9. Lambda Action Func练习

    namespace lambda { delegate void TestDelegate(string s); class Program { static void Main(string[] a ...

随机推荐

  1. ios开发--网页中调用JS与JS注入

    先将网页弄到iOS项目中: 网页内容如下, 仅供测试: <html> <head> <meta xmlns="http://www.w3.org/1999/xh ...

  2. Centos后台运行jar

    jar后台运行 nohup java -jar xx.jar >/dev/null & 此处的">/dev/null"作用是将终端输出信息输出到空洞中,即不保存 ...

  3. Android安全问题 抢先拦截短信

    同上篇文章一样,这里只陈述结果,代码分析稍后给出 导读:本文叙述如何先于某些伪杀毒软件.病毒.常规软件获取到短信 众所周知,android系统在收到短信息的时候会发送广播,但是此广播是有序广播,也就是 ...

  4. MySQL数据库服务器安装标准

    MySQL数据库服务器安装标准 (1).BIOS优化,阵列配置 1.1:关闭CPU节能,因为服务器品牌众多,BIOS设置不相同,主要是关闭CPU节能,如C1,DELLR730,已经智能设置,直接有个p ...

  5. Vim 配色设置与配色脚本语法

    几个给tag加颜色的插件 https://github.com/octol/vim-cpp-enhanced-highlight:基于tag的c family语法高亮 https://github.c ...

  6. 1019.Line Painting(线段树 离散化)

    1019 离散化都忘记怎么写了 注意两个端点 离散化后用线段树更新区间 混色为-1  黑为2  白为1  因为N不大 最后直接循环标记这一段的颜色查找 #include <iostream> ...

  7. bzoj1056: [HAOI2008]排名系统 && 1862: [Zjoi2006]GameZ游戏排名系统

    hash 加上 平衡树(名次树). 这道题麻烦的地方就在于输入的是一个名字,所以需要hash. 这个hash用的是向后探查避免冲突,如果用类似前向星的方式避免冲突,比较难写,容易挂掉,但也速度快些. ...

  8. Vs 引用第三方DLL文件 版本不一致问题 (npoi与memcached中的ICSharpCode.SharpZipLib版本冲突的解决方案)

    最近在 做 MailChimp 与网站功能 集成时,发现 MailChimp 2API 中的 MailChimp.dll  中的依赖项 SerivceStack.Text.dll (版本为3.9.71 ...

  9. amoeba安装与实现amoeba for mysql读写分离

    运行环境 l  CentOS6.3 l  Jdk1.6.0_30 l  amoeba-mysql-binary-2.2.0 l  amoeba:192.168.88.17 l  master1:192 ...

  10. 【转】Android AlertDialog 点击对话框外部区域不关闭的设置

    原文网址:http://blog.sina.com.cn/s/blog_8f1c79dd0101a63u.html 在Android开发中,常常需要调用对话框,但会遇到这样一种情况,在显示对话框的时候 ...