一、【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. 第38章:MongoDB-集群--Replica Sets(副本集)---多机的搭建

    ①机器环境 182.48.115.236    master-node(主节点) 182.48.115.237    slave-node1(从节点) 182.48.115.238    slave- ...

  2. Python简介及环境安装

    Python 官网传送门 Python是一种面向对象的解释性计算机程序设计语言. Python 2.7将于2020年1月1日终止支持,本笔记基于Python3. pip pip 是一个现代的,通用的 ...

  3. array_filter()函数

    用回调函数过滤数组中的值 array_filter(array,callbackfunction); 返回过滤后的数组

  4. 进度条(progress_bar)

    环境:linux.centos6.5 #include<stdio.h> #include<unistd.h> int main() { ]={'\0'}; char ch[] ...

  5. 单源最短路径算法——Bellman-ford算法和Dijkstra算法

     BellMan-ford算法描述 1.初始化:将除源点外的所有顶点的最短距离估计值 dist[v] ← +∞, dist[s] ←0; 2.迭代求解:反复对边集E中的每条边进行松弛操作,使得顶点集V ...

  6. Android开发工程师文集-相关控件的讲解,五大布局

    前言 大家好,给大家带来Android开发工程师文集-相关控件的讲解,五大布局的概述,希望你们喜欢 TextView控件 TextView控件有哪些属性: android:id->控件的id a ...

  7. 开源性能测试工具Locust使用篇(一)

    1. 环境准备 安装python3.6 ,安装步骤略 pip install locust 安装完成后使用locust -V检查 2.locust使用,先编辑一个简单的load_test.py的脚本 ...

  8. Netty:Channel 建立后消息发送失败

    1. 问题现象 Channel 建立后消息发送失败: ChannelFuture future = DeviceManager.getBootstrap().connect(); deviceChan ...

  9. MySQL 字符集utf8和utf-8的关系

    目录 什么是字符集(character set) 校对规则(collation) ASCII码 Unicode国际化支持 UTF-8 utf8 utf8与utf8mb4的关系 超集 字符集设置 什么是 ...

  10. 声反馈抑制使用matlab/simulink仿真

    第一份工作时做啸叫抑制的仿真,调大0.3可以有大的啸叫产生,下图的SIMULINK仿真模型 实现移相有多种方法: 1.iir实现 2.FFT实现 3.使用FIR实现 所有信号均可以由正弦信号叠加而成.