(C#) Action, Func, Predicate 等泛型委托
首先需要理解 Delegate, Action, Func, Predicate 從根本原理上講都是函數指針,只不過傳入參數和返回值有所限定。
所有的Action, Func, Predicate 都可以轉化成 delegate 來實現。
(1). delegate
delegate我们常用到的一种声明
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。
例:public delegate string MethodDelegate(int x, int y);表示有两个参数,并返回string型。
(2). Action
Action是无返回值的泛型委托。
Action 表示无参,无返回值的委托
Action<int,string> 表示有传入参数int,string无返回值的委托 = delegate void MethodDelegate(int num, string str);
Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托
Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托
Action至少0个参数,至多16个参数,无返回值。
例:
public void Test<T>(Action<T> action,T p)
{
action(p);
}
(3). Func
Func是有返回值的泛型委托
Func<int> 表示无参,返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托 = delegate int MethodDelegate(object obj, string str);
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托
Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void
例:
public int Test<T1,T2>(Func<T1,T2,int>func,T1 a,T2 b)
{
return func(a, b);
}
(4) .predicate
predicate 是返回bool型的泛型委托
predicate<int> 表示传入参数为int 返回bool的委托 = delegate bool MethodDelegate(int num);
Predicate有且只有一个参数,返回值固定为bool
例:public delegate bool Predicate<T> (T obj)
參考:
https://www.cnblogs.com/akwwl/p/3232679.html
(C#) Action, Func, Predicate 等泛型委托的更多相关文章
- Predicate<T>与Func<T, bool>泛型委托
引用别人的: static void Main(string[] args) { List<string> l = new List<string>(); l.Add(&quo ...
- C#中Predicate<T>与Func<T, bool>泛型委托的用法实例
本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: 1 2 3 4 5 6 ...
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...
- 【Unity|C#】基础篇(11)——内置的泛型委托(Action/Func/Predicate)
[Action] 无返回值 的泛型委托,可以有0~16个参数(函数重载) public delegate void Action(); // 无参数 public delegate void Acti ...
- c# Action,Func,Predicate委托
System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...
- 委托delegate,Action,Func,Predicate
C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...
- C# 委托应用总结(委托,Delegate,Action,Func,predicate)
C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...
- Func<T,TResult>泛型委托
描述: 封装一个具有一个参数并返回TResult参数指定的类型值的方法. 语法: public delegate TResult Func<T,TResult>(T arg); 参数类型: ...
- Delegate,Action,Func,Predicate的使用与区别
C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...
随机推荐
- 【渗透测试学习平台】 web for pentester -2.SQL注入
Example 1 字符类型的注入,无过滤 http://192.168.91.139/sqli/example1.php?name=root http://192.168.91.139/sqli/e ...
- leetcode 110 Balanced Binary Tree ----- java
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 顺序表及其多种实现方式 --- C/C++
所谓顺序表,即线性表的顺序存储结构.下面给出的是数据结构---线性表的定义. ADT List{ 数据对象: 线性表的数据对象的集合为{a1,a2,a3,...,an},每个元素的类型为ElemTyp ...
- 【NOIP2013】货车运输
感觉这题挺水的……真的挺水的…… 原题: A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货物,司机们想知道每辆车 ...
- ES VS Hbase
http://db-engines.com/en/system/Elasticsearch%3BHBase
- IntelliJ IDEA currently
https://www.jetbrains.com/help/idea/2016.2/creating-a-project-from-scratch.html https://www.jetbrain ...
- java linux book
calvin1978.blogcn.com/articles/javabookshelf.html
- EasyUI之DataGrid使用
http://blog.csdn.net/liovey/article/details/9173931 背景介绍: 原 先项目采用普通的jsp页面来做为前端显示,用户体验差,并且为了实现某一种效果需要 ...
- ABBYY FineReader的快速任务功能如何用
在使用ABBYY FineReader Pro for Mac OCR文字识别软件处理文档时,经常会执行一系列相同的步骤,如扫描.识别.将已识别文本导出为特定格式或导出至特定应用程序.针对经常执行的任 ...
- html之texteara
定义多行的文本输入控件,所有浏览器都支持,可容纳无限的文本,等宽的字体. 用css的height和width来设置其框框的大小是个很好的办法,其中的文本换行符为%OD/%OA(回车换行) html5中 ...