转自pnljs 委托(Func<int,bool>)
随笔- 147 文章- 0 评论- 16
Func的介绍
经常看到 Func<int, bool>...这样的写法,看到这样的就没有心思看下去了。我们学技术还是需要静下心来。
对Func<int,bool>的Func转到定义看它的解释:
// 摘要:
// 封装一个具有一个参数并返回 TResult 参数指定的类型值的方法。
//
// 参数:
// arg:
// 此委托封装的方法的参数。
//
// 类型参数:
// T:
// 此委托封装的方法的参数类型。
//
// TResult:
// 此委托封装的方法的返回值类型。
//
// 返回结果:
// 此委托封装的方法的返回值。
[TypeForwardedFrom("System.Core, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=b77a5c561934e089")]
public delegate TResult Func<in T, out TResult>(T arg);
in T 代表输入参数 1 out TResult 表示输出参数 2 再看返回值是 TResult 3 构造方法需要的参数是T 4
1与4,2与3进行对比,你发现了什么?!参数类型一样对吧。 5
Func是一个委托,委托里面可以存方法,那我们就来建一个与之匹配的方法: 以Func<int,bool>为例:
private bool IsNumberLessThen5(int number)
{return number < 5;}
Func<int,bool> f1 = IsNUmberLessThen5;
调用: bool flag= f1(4);
以下是具体代码:
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Func<int, bool> f1 = IsNumberLessThen5; bool flag = f1(4); Console.WriteLine(flag);
//以下是其它的用法,与IsNumberLessThen5作用相同。只是写法不同而已。 Func<int, bool> f2 = i => i < 5; Func<int, bool> f3 = (i) => { return i < 5; }; Func<int, bool> f4 = delegate(int i) { return i < 5; }; flag = f2(4); Console.WriteLine(flag); flag = f3(4); Console.WriteLine(flag); flag = f4(4); Console.WriteLine(flag);
Console.ReadLine(); }
private static bool IsNumberLessThen5(int number) { if (number < 5) return true; return false; } } }
转自pnljs 委托(Func<int,bool>)的更多相关文章
- .NET (二)委托第二讲:内置委托Func
在上一章节中,我们自己声明了一个委托: public delegate bool Cal(int num); 接受int参数,返回bool类型,目的是过滤集合中的 奇数 或者 偶数. .NET 为我们 ...
- Expression<Func<TObject, bool>>与Func<TObject, bool>的区别
Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后 ...
- lambda表达式Expression<Func<Person, bool>> 、Func<Person, bool>区别
前言: 自己通过lambda表达式的封装,将对应的表达式转成字符串的过程中,对lambda表达式有了新的认识 原因: 很多开发者对lambda表达式Expression<Func<Pers ...
- C#学习笔记(30)——系统自带委托Func和Action
说明(2017-11-23 10:46:33): 1. Func有返回值,Action无返回值,以后就不用定义delegate委托了. 2. 不过还是不知道什么时候该用委托,蒋坤在讲完事件后,留了个作 ...
- Expression<Func<T, bool>>与Func<T, bool>的区别
转自:http://www.cnblogs.com/wow-xc/articles/4952233.html Func<TObject, bool>是委托(delegate) Expres ...
- EF Core 封装方法Expression<Func<TObject, bool>>与Func<TObject, bool>区别
unc<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后就 ...
- (转)C#中的Predicate<T>与Func<T, bool>
Delegate至少0个参数,至多32个参数,可以无返回值,也可以指定返回值类型.这个是祖宗. Func可以接受0个至16个传入参数,必须具有返回值. Action可以接受0个至16个传入参数,无 ...
- Predicate<T>与Func<T, bool>泛型委托
引用别人的: static void Main(string[] args) { List<string> l = new List<string>(); l.Add(&quo ...
- C#中Predicate<T>与Func<T, bool>泛型委托的用法实例
本文以实例形式分析了C#中Predicate<T>与Func<T, bool>泛型委托的用法,分享给大家供大家参考之用.具体如下: 先来看看下面的例子: 1 2 3 4 5 6 ...
随机推荐
- OAF_开发系列24_实现OAF更新记录显示Record History(案例)
20150716 Created By BaoXinjian
- OAF_开发系列10_实现OAF动态LOV设定
20150712 Created By BaoXinjian
- c++中的宏 #define _CLASSDEF(name) class name
#include <iostream> using namespace std; #define _CLASSDEF(name) class name; \ typedef name * ...
- 汇编语言学习与Makefile入门
继续开发 ; hello-os ; TAB= ORG 0x7c00 ; 指明程序的装载地址 ; 以下的记述用于标准FAT12格式的软盘 JMP entry DB 0x90 DB "HELLO ...
- 彻底搞定char/wchar_t/unicode
彻底搞定char/wchar_t!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! (2013-07-17 10:18:28) 转载▼ 从char/wchar_t到TCHAR(1) ...
- mysql数据库的一些基本操作
下面列出一些做项目时常用到的一些mysql操作. 1.对数据库的操作 查看所有的数据库:show databases; 新建一个数据库:create database database_name; 此 ...
- Centos6.4 aria2 webui-aria2
wget http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm rpm -ivh ...
- C++设计模式-Composite组合模式
Composite组合模式作用:将对象组合成树形结构以表示“部分-整体”的层次结构.Composite使得用户对单个对象和组合对象的使用具有一致性. UML图如下: 在Component中声明所有用来 ...
- firebug常用工具
1.console.group("第一组"); console.log(1); console.groupend(); 2.console.dir(对象);//输出对象的所有信息 ...
- python平台跨平台开发
有助于跨平台开发的 os 模块属性: linesep 用于在文件中分隔行的字符串 sep 用来分隔文件路径名的字符串 pathsep 用于分隔文件路径的字符串 curdir 当前工作目录的字符串 ...