C#中的Action和Func和Predicate
一、【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<参数,返回值>变量名=函数名
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
{ }
}
}
}
表示定义一组条件并确定指定对象是否符合这些条件的方法。此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素。
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的更多相关文章
- 使用.NET中的Action及Func泛型委托
委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调 ...
- 委托学习续:Action、Func和Predicate
我们先看一个上一章的委托的例子: using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- .NET中的Action及Func泛型委托
委托,在C#编程中占有极其重要的地位,委托可以将函数封装到委托对象中,并且多个委托可以合并为一个委托,委托对象则可以像普通对象一样被存储.传递,之后在任何时刻进行调用,因此,C#中函数回调机制的实现基 ...
- Aap.Net中的Action和Func委托
前言 最近在阅读某开源框架源码的时候,发现作者在其中运用了很多Action委托和Func委托,虽然我之前在项目中也有一些对委托的实操,但还是免不了长时间的不用,当初消化的一些委托基础都遗忘了...索性 ...
- C#基础知识六之委托(delegate、Action、Func、predicate)
1. 什么是委托 官方解释 委托是定义方法签名的类型,当实例化委托时,您可以将其实例化与任何具有兼容签名的方法想关联,可以通过委托实例调用方法. 个人理解 委托通俗一点说就是把一件事情交给别人来帮助完 ...
- C#委托的介绍(delegate、Action、Func、predicate)
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Deleg ...
- C#委托的介绍(delegate、Action、Func、predicate) --转载
来源:http://www.cnblogs.com/akwwl/p/3232679.html 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1 ...
- C#委托(Action、Func、predicate)
Predicate 泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素. public delegate boo ...
- 【转】C# 委托的介绍(delegate、Action、Func、predicate)
委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 1.委托的声明 (1). delegate delegate我们常用到的一种声明 Delegat ...
随机推荐
- vue中鼠标移入字体下面显示颜色并改变字体颜色的问题
<template> <div class="smart_nav" :class="{'fixedTop':fixedTop}"> &l ...
- Office365 Manager Plus之报表
Office365 Manager Plus之报表 也许您刚刚开始使用Office 365,对它的各个组件还有很多疑问,如何快速掌握Office 365各种服务的用法?如何管理邮箱?如何监控邮件流量? ...
- ABP框架系列之五十四:(XSRF-CSRF-Protection-跨站请求伪造保护)
Introduction "Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a maliciou ...
- Python_day6
Δ汉诺塔例子 # 定义三个全局的列表 la = [] lb = [] lc = [] stepn = 0 def create_hano(n): '''构建由n个圆盘组成的汉诺塔''' global ...
- ubuntu 配置jdk报错解决办法
vi /etc/profile ,添加如下代码 export JAVA_HOME=/home/mark/android/jdk1.8 export JRE_HOME=/home/mark/androi ...
- Beta阶段Scrum 冲刺博客合集
Beta阶段博客链接集合 第一篇Scrum冲刺博客 第二篇Scrum冲刺博客-Day1 第三篇Scrum冲刺博客-Day2 第四篇Scrum冲刺博客-Day3 第五篇Scrum冲刺博客-Day4 第六 ...
- 【pycharm 密钥】pycharm 2017 密钥
server选项里边输入: http://idea.liyang.io 亲测可用!!!
- Cura - CuraEngine - 架构分析
参考: https://blog.csdn.net/justdoithai/article/details/52746094
- attempt to open datawindow failed@安装两个PB软件
attempt to open datawindow failed@安装两个PB软件 安装了不同版本的PB软件后,默认打开方式为后一个安装的版本. 如果想改为第一个安装版本,在“右键”——>”打 ...
- python多版本管理包pyenv
前言: CentOS7 默认版本 python2.7.5 ,但是目前项目开发都是在python3的环境下,而单独编译安装python3时会出现各种异常!!! 系统默认python2.7又不能删除,否 ...