一、【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. oracle删除dbf导致的oracle工具不能正常使用

    1.使用cmd命令登录Oracle:sqlplus / as sysdba;就可以,中间两个空格.2.删除了dbf导致Oracle工具不能正常使用解决办法(oracle initialization ...

  2. OKR 与 KPI

    作者:Cat Chen链接:https://www.zhihu.com/question/22478049/answer/23833548来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业 ...

  3. IText实现对PDF文档属性的基本设置

    一.Itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  4. discover功能--自动发现主机

    discover功能,让监控的管理更加管理和自动化,现在来演示下如何使用discover功能自动发现主机 1.配置discover 2.在被侦测机上安装zabbix agent 安装完成后,修改配置( ...

  5. Latex命令

    .tex代码中 |   在pdf文档中 空一行  代表回车,下一行空两格 // 代表回车,下一行顶格

  6. unbuntu14.04下的串口软件monicom的使用

    上篇文章写到了将esp-idf中的examples里的helloworld烧写进了esp32的flash里面,本文就讲讲这个例子的测试和一个项目工程的建立. 首先为了得到esp32输出的信息,需要一个 ...

  7. volatile和synchronized

    volatile是变量修饰符,而synchronized则作用于一段代码或方法:看如下三句get代码: int i1;              int geti1() {return i1;} vo ...

  8. React Native学习方法论

    这是我技术公众号的第一篇文章,也是React Native系列文章的第一篇,对我的文章感兴趣的可以加我微信16230091进行关注. 本文表面上讲React Native(以下简称RN),实际上对于学 ...

  9. Interview Common Sample Codes

    1. Quick Sort: int partition(int A[], int p, int r) { int x = A[r]; // Pivot element int i = p - 1; ...

  10. 谈谈我们对userAgent的看法,为什么爬虫中需要userAgent?

    首先打开浏览器,按 F12 进入控制台(Console),然后输入:navigator.userAgent,即可看到 UA.例如: 1 2 Mozilla/5.0 (Windows NT 10.0; ...