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

 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. 搜索引擎spam

    什么是SPAM?搜索引擎优化中的SPAM 在搜索引擎优化相关的文章中中经常遇到“SPAM”一词,SPAM通常用来指垃圾邮件,不过在搜索引擎营销中所说的SPAM是专门针对那些欺骗搜索引擎的信息.搜索引擎 ...

  2. Linux/Unix 系统分析命令速查手册

    1.Hardware CPU information: cat /proc/cpuinfo 物理core个数: 统计core 逻辑CPU个数:统计processor Memory informatio ...

  3. 【HDOJ】4326 Game

    1. 题目描述一个长度为n个队列,每次取队头的4个人玩儿游戏,每个人等概率赢得比赛.胜者任然处在队头,然而败者按照原顺序依次排在队尾.连续赢得m场比赛的玩家赢得最终胜利.求第k个人赢得最终胜利的概率. ...

  4. 254 shades of grey

    254 shades of grey Description: Why would we want to stop to only 50 shades of grey? Let's see to ho ...

  5. 将非WPF window设为 WPF Window的Owner

    如果WPF Content是寄宿在Win32 窗体或Windows Form中,则在WPF模块中可能不会存在WPF Window(WPF模块的根可能是个UserControl).如果在WPF模块中弹出 ...

  6. UVA 12661 Funny Car Racing 有趣的赛车比赛(最短路,变形)

    题意:赛道有n个交叉点,和m条单向路径(有重边),每条路都是周期性关闭的,且通过仍需一段时间.在比赛开始时,所有道路刚好打开,选择进入该道路必须满足“在打开的时间段进入,在关闭之前出来”,即不可在路上 ...

  7. jwplayer 网页在线播放插件

    1.到官网 https://www.jwplayer.com/ 注册,取得key并下载免费版本(免费版只支持mp4格式): 2.编辑如下网页即可在线播放: <!DOCTYPE html> ...

  8. Entity Framework 增删改查和事务操作

    1.增加对象 DbEntity db = new DbEntity(); //创建对象实体,注意,这里需要对所有属性进行赋值(除了自动增长主键外),如果不赋值,则会数据库中会被设置为NULL(注意是否 ...

  9. 关于C#中static静态变量

    C#静态变量使用static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明的变量称做非静态变量,在对象被实例化时创建,通过对象进行访问一个类的所有实例的同一C#静 ...

  10. HDU 4521-小明序列(线段树好题)

    题意: n个数字的序列,求各数位置间隔大于d的最长上升子序列 分析: 最基本的dp但是数据量大O(n^2)肯定超时 前dp[i]为的最长上升子序列是由前dp[1]---dp[i-d-1]符合条件的最大 ...