一、【action<>】指定那些只有输入参数,没有返回值的委托

用了Action之后呢:

就是相当于省去了定义委托的步骤了。

演示代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace EventDemo
{
class Program
{
public delegate void myDelegate(string str);
public static void HellowChinese(string strChinese)
{
Console.WriteLine("Good morning," + strChinese);
Console.ReadLine();
} static void Main(string[] args)
{
//Delegate的代码
myDelegate d = new myDelegate(HellowChinese);
d("Mr wang"); //用了Action之后呢
Action<string> action = HellowChinese;
action("Spring."); Console.ReadLine();
}
}
}

二、func<> 这个和上面的那个是一样的,区别是这个有返回值!

语法:

Func<参数,返回值>变量名=函数名

Lambda表达式的调用方式
语法:(显示类型的参数列表)=>{语句}
eg:
Func<int,int,string>func=(x,y)=>(x*y).Tostring();
Console.WriteLine(fun(5,20));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace EventDemo
{
class Program
{
static void Main(string[] args)
{
//类似委托功能
Func<string, int> test = TsetMothod;
Console.WriteLine(test(""));
Func<string, int> test1 = TsetMothod; //只需要调用这个类就可以减少重复的代码
CallMethod<string>(test1, "");
//或者采用这种
CallMethod<string>(new Func<string, int>(TsetMothod), "");
CallMethod(new Func<string, int>(TsetMothod), ""); Func<int, double, decimal, string> testFun = TestFun;
double b = 2.3;
decimal c = 666.7m;
string strtestFun = testFun(, b, c);
Console.WriteLine("Func<int, double, decimal, string> testFun={0}", strtestFun); Console.ReadKey();
} public static string TestFun(int a, double b, decimal c)
{
return "TestFun";
} public static int TsetMothod(string name)
{
if (string.IsNullOrEmpty(name))
{
return ;
}
return ;
} public static void CallMethod<T>(Func<T, int> func, T item)
{
try
{
int i = func(item);
Console.WriteLine(i);
}
catch (Exception e)
{ }
finally
{ }
}
}
}
Predicate 泛型委托
  表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。
 
public delegate bool Predicate<T>(T obj);
类型参数介绍:
   T: 要比较的对象的类型。
   obj: 要按照由此委托表示的方法中定义的条件进行比较的对象。
   返回值:如果 obj 符合由此委托表示的方法中定义的条件,则为 true;否则为 false。
看下面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace EventDemo
{
class Program
{
static void Main(string[] args)
{
List<string> list = new List<string>() { "Mike", "Rose", "Steve" };
var mike = list.Find(new Predicate<string>(HaveLengthFive));
Console.WriteLine(mike);
Console.ReadLine();
}
static bool HaveLengthFive(string value)
{
return value.Length == ;
}
}
}

延伸:
  除了上面提到的外,你完全可以使用Predicate 定义新的方法,来加强自己代码。

public class GenericDelegateDemo
{
List<String> listString = new List<String>()
{
"One","Two","Three","Four","Fice","Six","Seven","Eight","Nine","Ten"
}; public String GetStringList(Predicate<String> p)
{
foreach(string item in listString)
{
if (p(item))
return item;
}
return null;
} public bool ExistString()
{
string str = GetStringList((c) => { return c.Length <= && c.Contains('S'); });
if (str == null)
return false;
else
return true;
}
}
 
 

C#中的Action和Func和Predicate的更多相关文章

  1. 使用.NET中的Action及Func泛型委托

          委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调 ...

  2. 委托学习续:Action、Func和Predicate

    我们先看一个上一章的委托的例子: using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  3. .NET中的Action及Func泛型委托

    委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调机制的实现基 ...

  4. Aap.Net中的Action和Func委托

    前言 最近在阅读某开源框架源码的时候,发现作者在其中运用了很多Action委托和Func委托,虽然我之前在项目中也有一些对委托的实操,但还是免不了长时间的不用,当初消化的一些委托基础都遗忘了...索性 ...

  5. C#基础知识六之委托(delegate、Action、Func、predicate)

    1. 什么是委托 官方解释 委托是定义方法签名的类型,当实例化委托时,您可以将其实例化与任何具有兼容签名的方法想关联,可以通过委托实例调用方法. 个人理解 委托通俗一点说就是把一件事情交给别人来帮助完 ...

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

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明   Deleg ...

  7. C#委托的介绍(delegate、Action、Func、predicate) --转载

    来源:http://www.cnblogs.com/akwwl/p/3232679.html 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1 ...

  8. C#委托(Action、Func、predicate)

    Predicate 泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素. public delegate boo ...

  9. 【转】C# 委托的介绍(delegate、Action、Func、predicate)

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Delegat ...

随机推荐

  1. Java中equals方法简略描述

    所有类都从Object中继承了equals方法,源码:public boolean equals(Object o){return this == o;} 直接判断this与o本身是否为同一对象(是否 ...

  2. C++获取工程路径、exe路径

    编码过程中有时候会用到获取工程所在路径或者exe所在的路径信息,这里稍微记录下. 获取工程路径 char pBuf[MAX_PATH]; //存放路径的变量 GetCurrentDirectory(M ...

  3. android 学习网站

    菜鸟教程 http://www.runoob.com/android/android-tutorial.html Android基础入门教程  http://www.runoob.com/w3cnot ...

  4. using python read/write HBase data

    A. operations on Server side 1. ensure hadoop and hbase are working properly 2. install thrift:  apt ...

  5. mysql最基础命令

    1:使用SHOW语句找出在服务器上当前存在什么数据库:mysql> SHOW DATABASES;2:2.创建一个数据库MYSQLDATAmysql> Create DATABASE MY ...

  6. Runtime之成员变量&属性&关联对象

    上篇介绍了Runtime类和对象的相关知识点,在4.5和4.6小节,也介绍了成员变量和属性的一些方法应用.本篇将讨论实现细节的相关内容. 在讨论之前,我们先来介绍一个很冷僻但又很有用的一个关键字:@e ...

  7. 8. Filters in ASP.NET MVC 5.0【ASP.NET MVC 5.0中的过滤器】

    ASP.NET Filers用来在MVC框架的不同请求处理阶段,注入额外的逻辑.过滤器为横切关注点提供了一种方法(日志记录,授权,缓存). 在这篇文章中,我将会向你介绍MVC框架支持的各种不同种类过滤 ...

  8. 方向导数,梯度和梯度下降之BGD,SGD

    方向导数和梯度的直观理解,from知乎-马同学: https://www.zhihu.com/question/36301367 BGD,SGD: https://www.cnblogs.com/gu ...

  9. 移动端IM开发者必读(一):通俗易懂,理解移动网络的“弱”和“慢”

    本系列文章引用了腾讯技术专家樊华恒<海量之道系列文章之弱联网优化>的部分章节,感谢原作者. 1.前言 随着移动互联网的高速发展,移动端IM以移动网络作为物理通信载体早已深入人心,这其中的成 ...

  10. axios的Get和Post方法封装及Node后端接收数据

    最近有做一个Vue的小项目,其中用到了尤大大推荐使用的axios,但是使用的过程中遇到了各种各样的问题,所以这次也是将一些心得分享出来. 安装的流程我就简单说一下下吧,在一个自己新建的文件夹中命令行中 ...