C#委托的介绍(delegate、Action、Func、predicate)

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

  1.委托的声明

  (1). delegate

    delegate我们常用到的一种声明

   Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型。

   例:public delegate int MethodtDelegate(int x, int y);表示有两个参数,并返回int型。

  (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个参数,无返回值。

   例:

    public void Test<T>(Action<T> action,T p)
{
action(p);
}

  (3). Func

   Func是有返回值的泛型委托

   Func 表示无参,返回值为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

  例:   

    public int Test<T1,T2>(Func<T1,T2,int>func,T1 a,T2 b)
{
return func(a, b);
}
(4) .predicate

   predicate 是返回bool型的泛型委托

   predicate 表示传入参数为int 返回bool的委托

   Predicate有且只有一个参数,返回值固定为bool

   例:public delegate bool Predicate (T obj)

  

  2.委托的使用

  (1).Delegate的使用  

复制代码

复制代码

public delegate int MethodDelegate(int x, int y);

private static MethodDelegate method;

static void Main(string[] args)

{

method = new MethodDelegate(Add);

Console.WriteLine(method(10,20));

Console.ReadKey();

}

    private static int Add(int x, int y)
{
return x + y;
}

复制代码

复制代码

  (2).Action的使用   

复制代码

复制代码

static void Main(string[] args)

{

Test(Action,"Hello World!");

Test(Action, 1000);

Test(p => { Console.WriteLine("{0}", p); }, "Hello World");//使用Lambda表达式定义委托

Console.ReadKey();

}

public static void Test(Action action, T p)

{

action(p);

}

private static void Action(string s)

{

Console.WriteLine(s);

}

private static void Action(int s)

{

Console.WriteLine(s);

}

复制代码

复制代码

  可以使用 Action<T1, T2, T3, T4> 委托以参数形式传递方法,而不用显式声明自定义的委托。 封装的方法必须与此委托定义的方法签名相对应。 也就是说,封装的方法必须具有四个均通过值传递给它的参数,并且不能返回值。 (在 C# 中,该方法必须返回 void)通常,这种方法用于执行某个操作。

  (3).Func的使用

复制代码

复制代码

static void Main(string[] args)

{

Console.WriteLine(Test<int,int>(Fun,100,200));

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 类的几种方法使用,用于在集合中搜索元素。

复制代码

复制代码

static void Main(string[] args)

{

Point[] points = { new Point(100, 200),

new Point(150, 250), new Point(250, 375),

new Point(275, 395), new Point(295, 450) };

Point first = Array.Find(points, ProductGT10);

Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);

Console.ReadKey();

}

private static bool ProductGT10(Point p)

{

if (p.X * p.Y > 100000)

{

return true;

}

else

{

return false;

}

}

复制代码

复制代码

  使用带有 Array.Find 方法的 Predicate 委托搜索 Point 结构的数组。如果 X 和 Y 字段的乘积大于 100,000,此委托表示的方法 ProductGT10 将返回 true。Find 方法为数组的每个元素调用此委托,在符合测试条件的第一个点处停止。

  3.委托的清空

  (1).在类中申明清空委托方法,依次循环去除委托引用。

     方法如下:

复制代码

复制代码

    public MethodDelegate OnDelegate;

public void ClearDelegate()

{

while (this.OnDelegate != null)

{

this.OnDelegate -= this.OnDelegate;

}

}

复制代码

复制代码

  (2).如果在类中没有申明清空委托的方法,我们可以利用GetInvocationList查询出委托引用,然后进行去除。  

  方法如下:

复制代码

复制代码

public MethodDelegate OnDelegate;

     static void Main(string[] args)

{

Program test = new Program();

        if (test.OnDelegate != null)
{
System.Delegate[] dels = test.OnDelegate.GetInvocationList();
for (int i = 0; i < dels.Length; i++)
{
test.OnDelegate -= dels[i] as MethodDelegate;
}
}
}

复制代码

复制代码

  4.委托的特点

  委托类似于 C++ 函数指针,但它们是类型安全的。

  委托允许将方法作为参数进行传递。

  委托可用于定义回调方法。

  委托可以链接在一起;例如,可以对一个事件调用多个方法。

  方法不必与委托签名完全匹配。

  5.总结:

  Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型

  Func可以接受0个至16个传入参数,必须具有返回值

  Action可以接受0个至16个传入参数,无返回值

  Predicate只能接受一个传入参数,返回值为bool类型

委托delegate,Action,Func,Predicate的更多相关文章

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

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

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

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

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

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

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

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

  5. c# Action,Func,Predicate委托

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

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

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

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

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

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

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

  9. 对委托 以及 action func 匿名函数 以及 lambda表达式的简单记录

    class Program { public delegate void MyDelegate(string str); static void Main(string[] args) { // My ...

随机推荐

  1. VMWare linux 打印太多,看不到之前的记录的解决方法总结

    1.在命令后面加 | more. 可以每次按空格键或是回车键后翻.2.命令后面加| less ,可以前后翻.3.用重定向到文件 > 文件名,之后慢慢看 ----待补充 ------

  2. three.js 流程图

    用Axure做了个模型图:          第一步: Scene --模型.灯光.特效 第二步: Camera --视角 第三步: Renderer -- 渲染输出 第四步: render --渲染 ...

  3. 【SQL】日期型函数

    1. SYSTATE 用来返回系统当前时间 SQL> select sysdate from dual; SYSDATE ------------------- 2017-03-03 09:49 ...

  4. win8使用教程

    win8如何关机 http://product.pconline.com.cn/itbk/software/win8/1305/3301394.html shutdown.exe -s -t 00 W ...

  5. 【sqli-labs】 less30 GET- Blind -Impidence mismatch -Having a WAF in front of web application (GET型基于盲注的带有WAF注入)

    这次是双引号的,WAF绕过方法不变 http://192.168.136.128/sqli-labs-master/Less-30/login.php?id=1&id=2" and ...

  6. html 底部虚线

    <div style="width: 100%; font-size: 14px; color: #666; border-bottom: 1px dashed #666;" ...

  7. ApplicationLoader登录失败

    报错:Please sign in with an app-specific password. You can create one at appleid.apple.com 是因为帐号开启了双重认 ...

  8. Android LinearLayout整个布局设置不可点击

    1,activity的xml布局(布局中有个Button按钮,点击按钮弹出一个popupwindow ) <?xml version="1.0" encoding=" ...

  9. Linux文件压缩命令笔记

    1.gzip/gunzip gzip/gunzip:主要是进行单个文件的压缩和解压缩的命令. 示例:gzip hello.txt #执行压缩hello.txt ls hello.txt.gz #查看文 ...

  10. lvs直接路由模式DR模式

    1)实验所有关闭防火墙systemtcl stop firewalldsystemctl disable firewalldsetenforce 0iptables -F2)配置负载调度器配置虚拟IP ...