一、【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. 【Node.js】安装及使用

    Node.js是在Chrome的V8 JavaScript引擎上构建的JavaScript运行时.Node.js使用事件驱动的非阻塞I / O模型,使其轻量且高效.Node.js的软件包生态系统npm ...

  2. Spring AOP配置方式

    AOP 面向切面编程,允许在 java 应用中的方法调用的前后做一些处理. 本文通过实例介绍两种主要的Spring AOP 配置方式:xml 方式配置,注解方式配置 XML 方式配置 1. 项目包类结 ...

  3. Django的学习进阶(一)—— 外键的使用

    一.描述 在利用django做网络开发的时候我们会遇到一个问题就是,我们建立了多张数据表,但是多张数据表中的内容是不一样的,但是之间有着联系比如: 我有两张表,一张是记录歌曲信息的内容,一张是对歌曲操 ...

  4. mysql 地理位置定位

    SET @pt2 = ST_GeomFromText('POINT(116.405289 39.904987)'); SELECT *,ST_Distance_Sphere(ST_GeomFromTe ...

  5. Note | 学术论文写作方法和技巧

    目录 1. 论文发表流程 2. 确定科研方向 3. 思考问题和解决问题 4. 审稿 5. 论文写作 5.1. 标题 5.2. 摘要 5.3.介绍 5.4. 相关工作 5.5. 段落 5.6. 方法 5 ...

  6. SPI通信协议(非原创,转载他人,用于学习)

    SPI通信协议:1.SPI主从模式:2.数据信号的相位与极性:3.数据帧的格式. 一.什么是SPI? SPI是串行外设接口(Serial Peripheral Interface)的缩写.是 Moto ...

  7. Linux 线程编程2.0——线程同步-互斥锁

    当我们需要控制对共享资源的存取的时候,可以用一种简单的加锁的方法来控制.我们可以创建一个读/写程序,它们共用一个共享缓冲区,使用互斥锁来控制对缓冲区的存取. 函数 pthread_mutex_init ...

  8. zookeeper学习day01

    1.zkAPI:(借助闭锁来实现)    1)创建闭锁对象  2)创建zk对象  3)连接zk客户端(连接成功执行countDown方法)  4)执行await方法(保证链接成功) 5)zk对象调用对 ...

  9. commons-text 生成指定长度的随机字符串

    package com.skylink.junge.demo; import java.util.HashSet; import java.util.Set; import org.apache.co ...

  10. Shiro 基础教程

    原文地址:Shiro 基础教程 博客地址:http://www.extlight.com 一.前言 Apache Shiro 是 Java 的一个安全框架.功能强大,使用简单的Java安全框架,它为开 ...