C#的委托(delegate、Action、Func、predicate)
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。事件是一种特殊的委托。
1.委托的声明
delegate我们常用到的一种声明
delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。
namespace ConsoleApplication1
{
class Program
{
delegate void NumDelegate(int num);
static void Main(string[] args)
{
AClass _a = new AClass();
_a.cwdelegate = _a.AddNum;
_a.cwdelegate();
NumDelegate num20delegate = new NumDelegate(Add20);
Console.WriteLine("-------------------------------------------");
NumDelegate adddelegate = new NumDelegate(_a.AddNum);
NumDelegate subdelegate = new NumDelegate(_a.SubNum);
NumDelegate num30delegate = adddelegate + subdelegate;
num30delegate();
Console.WriteLine("-------------------------------------------");
num30delegate += num20delegate;
num30delegate();
Console.WriteLine("-------------------------------------------");
num30delegate += adddelegate;
//去掉最后一个adddelegate
num30delegate -= adddelegate;
num30delegate();
Console.ReadLine();
}
static void Add20(int num)
{
Console.WriteLine(string.Format("Add20: {0}", num));
}
} class AClass
{
public int result = ;
public delegate void CWDelegate(int num);
public CWDelegate cwdelegate;
public void AddNum(int num)
{
Console.WriteLine(string.Format("AddNum: {0}", num));
} public void SubNum(int num)
{
Console.WriteLine(string.Format("SubNum: {0}", num));
}
}
}
2. Action
Action是无返回值的泛型委托。
Action 表示无参,无返回值的委托
Action<int,string> 表示有传入参数int,string无返回值的委托
Action<int,string,bool> 表示有传入参数int,string,bool无返回值的委托
Action<int,int,int,int> 表示有传入4个int型参数,无返回值的委托
Action至少0个参数,至多16个参数,无返回值。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
AClass _a = new AClass();
_a.cwdelegate = _a.AddNum;
_a.cwdelegate(); Console.WriteLine("-------------------------------------------");
Action<int> act1 = _a.SubNum;
Action<int> act2 = _a.AddNum;
Action<int> act3 = act1 + act2;
act3();
Console.WriteLine("-------------------------------------------");
act3 += _a.cwdelegate;
act3();
Console.WriteLine("-------------------------------------------");
act3 -= _a.cwdelegate;
act3();
Console.ReadLine();
}
static void Add20(int num)
{
Console.WriteLine(string.Format("Add20: {0}", num));
}
} class AClass
{
public int result = ;
public Action<int> cwdelegate;
public void AddNum(int num)
{
Console.WriteLine(string.Format("AddNum: {0}", num));
} public void SubNum(int num)
{
Console.WriteLine(string.Format("SubNum: {0}", num));
}
}
}
3. Func
Func是有返回值的泛型委托
Func<int> 表示无参,返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托
Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Test<int, int>(Fun, , ));
Console.ReadKey();
}
public static int Test<T1, T2>(Func<T1, T2, int> func, T1 a, T2 b)
{
return func(a, b);
}
private static int Fun(int a, int b)
{
return a + b;
}
}
4. predicate的使用
泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。
class Program
{
static void Main(string[] args)
{
int[] array = new int[] { , , , , , , , , , };
var first = Array.Find(array, ProductGT10);
Console.WriteLine("Found: X = {0}", first);
Console.ReadKey();
}
private static bool ProductGT10(int x)
{
return x % == ;
}
}
C#的委托(delegate、Action、Func、predicate)的更多相关文章
- Delegate,Action,Func,Predicate的使用与区别
C#4.0推出后,类似Linq,Lamda表达式等许多新的程序写法层次不穷.与之相关的Delegate,Action,Func,Predicate的使用和区别也常常让大家迷惑,此处就结合实际的应用,对 ...
- 委托delegate,Action,Func,Predicate
C#委托的介绍(delegate.Action.Func.predicate) 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 ...
- C# 委托应用总结(委托,Delegate,Action,Func,predicate)
C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...
- (C#) Action, Func, Predicate 等泛型委托
(转载网络文章) (1). delegate delegate我们常用到的一种声明 Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型. 例:public del ...
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...
- Delegate,Action,Func,匿名方法,匿名委托,事件
一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本的数据类型(或者没有参数),比如 public void HelloWorld ...
- c# Action,Func,Predicate委托
System命名空间下已经预先定义好了三中泛型委托,Action,Func和Predicate,这样我们在编程的时候,就不必要自己去定义这些委托了 Action是没有返回值的 Func是带返回值的 不 ...
- 【Unity|C#】基础篇(11)——内置的泛型委托(Action/Func/Predicate)
[Action] 无返回值 的泛型委托,可以有0~16个参数(函数重载) public delegate void Action(); // 无参数 public delegate void Acti ...
- 委托,C#本身的委托(Action Func)
1.Action 分为带泛型的和不带泛型的,带泛型可传入任何类型的参数. 格式如下: using System; using System.Collections.Generic; using Sys ...
- 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件
Tks: http://www.cnblogs.com/yjmyzz/archive/2009/11/23/1608818.html 20150801 add: http://www.cnblogs. ...
随机推荐
- Eclipse Groovy插件使用时出现的错误 org.eclipse.core.runtime.InvalidRegistryObjectException: Invalid registry object
在eclipse marketplace中下载了groovy插件,发现使用的groovy版本跟项目中使用的groovy版本不一致. 于是在Preferences -> Groovy -> ...
- CircleList-使用UGUI实现的圆形列表
CircleList CircleList是一个通过UGUI实现的圆形列表,通过缩放.平移和层级的改变模拟一个3D的圆形列表. 效果 添加与旋转 间距调整 椭圆形的旋转 参数 CenterX: 椭圆圆 ...
- IEEE 802.1X标准
1.介绍 802.1X是一个IEEE标准,通过对用户进行基于端口的安全认证和对密钥的动态管理,从而实现保护用户用户的位置隐私和身份隐私以及有效保护通信过程中信息安全的目的. 在802.1X协议中,只有 ...
- 51nod1222 最小公倍数计数
题目来源: Project Euler 基准时间限制:6 秒 空间限制:131072 KB 分值: 640 定义F(n)表示最小公倍数为n的二元组的数量. 即:如果存在两个数(二元组)X,Y(X & ...
- Linux - 系统资源
查看剩余内存 free -m #-/+ buffers/cache: #6458M为真实使用内存 1649M为真实剩余内存(剩余内存+缓存+缓冲器) #linux会利用所有的剩余内存作为缓存,所以要保 ...
- 2016年1月7日 隐藏NavigationBar时的一个坑
http://www.jianshu.com/p/efb960fed457 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear: ...
- 洛谷 P2257 YY的GCD
洛谷 P2257 YY的GCD \(solution:\) 这道题完全跟[POI2007]ZAP-Queries (莫比乌斯反演+整除分块) 用的一个套路. 我们可以列出答案就是要我们求: \(ans ...
- vscode 配置Git
步骤: 下载Git客户端 配置环境变量 设置vscode与Git的关联 重启 步骤一: 该网址,下载即可. https://git-scm.com/downloads 步骤二: 计算机 > 属性 ...
- 【centos】 error: command 'gcc' failed with exit status 1
原文连接http://blog.csdn.net/fenglifeng1987/article/details/38057193 用安装Python模块出现error: command 'gcc' f ...
- .NET中制做对象的副本(三)通过序列化和反序列化为复杂对象制作副本
1.类的定义 /// <summary> /// 学生信息 /// </summary> [Serializable] public class Stu { /// <s ...