委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递。事件是一种特殊的委托。

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

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

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

  2. 委托delegate,Action,Func,Predicate

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

  3. C# 委托应用总结(委托,Delegate,Action,Func,predicate)

    C# 委托应用总结 一.什么是委托 1.1官方解释 委托是一种定义方法签名的类型.当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联.您可以通过委托实例调用方法. 1.2个人理解 委托就是执 ...

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

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

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

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

  6. Delegate,Action,Func,匿名方法,匿名委托,事件

    一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本的数据类型(或者没有参数),比如 public void HelloWorld ...

  7. c# Action,Func,Predicate委托

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

  8. 【Unity|C#】基础篇(11)——内置的泛型委托(Action/Func/Predicate)

    [Action] 无返回值 的泛型委托,可以有0~16个参数(函数重载) public delegate void Action(); // 无参数 public delegate void Acti ...

  9. 委托,C#本身的委托(Action Func)

    1.Action 分为带泛型的和不带泛型的,带泛型可传入任何类型的参数. 格式如下: using System; using System.Collections.Generic; using Sys ...

  10. 温故而知新:Delegate,Action,Func,匿名方法,匿名委托,事件

    Tks: http://www.cnblogs.com/yjmyzz/archive/2009/11/23/1608818.html 20150801 add: http://www.cnblogs. ...

随机推荐

  1. pycharm sql语句警告

    产生原因为没有配置数据库,配置数据库,似乎没什么作用 那么,直接去掉他的警告提示 找到setting->editor->inspections,然后找到右边sql下的 No data so ...

  2. JavaScript之柯里化

    //未柯里化 function add(a,b){ return a + b; } //柯里化 function add(y){ return function(x){ console.log(y + ...

  3. C语言文件操作详解

    C语言中没有输入输出语句,所有的输入输出功能都用 ANSI C提供的一组标准库函数来实现.文件操作标准库函数有: 文件的打开操作 fopen 打开一个文件 文件的关闭操作 fclose 关闭一个文件 ...

  4. python 入门基础24 元类、单例模式

    内容目录: 一.元类 二.单例模式 一.元类 1 什么是元类: 源自一句话:在python中,一切皆对象,而对象都是由类实例化得到的 class OldboyTeacher: def __init__ ...

  5. Adroid反编译资料收集

    Android反编译神器jadx的使用 https://blog.csdn.net/Fisher_3/article/details/78654450 Android 反编译利器,jadx 的高级技巧 ...

  6. sql 中的null值

    1.包含null的表达式都为空 select salary*12+nvl(bonus,0) nvl是虑空函数 2. null值永远!=null select  * from emp  where bo ...

  7. SpringBoot定制错误页面

    (1)有模板引擎的情况下,例如404错误,将会在thymeleaf的templates的error下寻找404.html,如果找不到再寻找4xx.html *所有4开头的错误状态码如果找不到特定的ht ...

  8. 允许远程用户登录访问mysql的方法

    需要手动增加可以远程访问数据库的用户. 方法一.本地登入mysql,更改 "mysql" 数据库里的 "user" 表里的 "host" 项 ...

  9. matplotlib 画图

    matplotlib 画图 1. 画曲线图       Tompson = np.array([0, 0, 0, 0, 0.011, 0.051, 0.15, 0.251, 0.35, 0.44, 0 ...

  10. mac 报错Root chmod operation not permitted on file

    系统:mac os 10.14.1 重启电脑 mac用户在升级系统之后,电脑启用了SIP(System Integrity Protection),增加了rootless机制,导致即使在root权限下 ...