c# 委托 Predicate的使用示例
一、说明
委托Predicate 可以有参数(比如下面的示例),也可以不带参数,委托Predicate是返回固定值bool值的委托
二、示例代码(控制台程序)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Learn1
{
class Program
{
static void Main(string[] args)
{
int ageState = ;
int claState = ;
int age = ;
string className = "1班";
List<Student> myList = new List<Student>() {
new Student(){ age=, className="2班", name="同学1"},
new Student(){ age=, className="1班", name="同学2"},
new Student(){ age=, className="3班", name="同学3"},
new Student(){ age=, className="3班", name="同学4"},
new Student(){ age=, className="1班", name="同学5"},
new Student(){ age=, className="1班", name="同学6"},
new Student(){ age=, className="1班", name="同学7"},
new Student(){ age=, className="2班", name="同学8"},
new Student(){ age=, className="2班", name="同学9"},
new Student(){ age=, className="1班", name="同学10"},
new Student(){ age=, className="1班", name="同学11"},
new Student(){ age=, className="1班", name="同学12"},
new Student(){ age=, className="2班", name="同学13"} };
List<Student> resultList = myList.FindAll(new Predicate<Student>(
delegate(Student stu)
{
bool IsOk = true;
if (ageState==)
{
if(stu.age<age)
{
IsOk = false;
}
} if(claState==)
{
if(stu.className!=className)
{
IsOk = false;
}
} return IsOk; } ));
resultList.ForEach(x => {
Console.WriteLine("名称:{0},年龄:{1},班级:{2}",x.name,x.age,x.className);
});
Console.ReadKey();
}
}
public class Student
{
public string name { get; set; }
public string className { get; set; }
public int age { get; set; }
}
}
c# 委托 Predicate的使用示例的更多相关文章
- C#泛型委托Predicate、Action、Func
Predicate Predicate泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素.通过查看源码发现 Pr ...
- C#的泛型委托Predicate/Func/Action
Predicate<T> 是一个委托,它代表了一个方法,它的定义是: namespace System { // 摘要: 表示定义一组条件并确定指定对象是否符合这些条件的方法. ...
- .NET (五)委托第五讲:内置委托Predicate
// 摘要: // 表示定义一组条件并确定指定对象是否符合这些条件的方法. // // 参数: // obj: // 要按照由此委托表示的方法中定义的条件进行比较的对象. // // 类型参数: // ...
- 泛型委托 Predicate/Func/Action
Predicate 泛型委托 表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素.看看下面它的定义: // Summar ...
- C#的泛型委托Predicate/Func/Action(转)
Predicate 泛型委托:表示定义一组条件并确定指定对象是否符合这些条件的方法.此委托由 Array 和 List 类的几种方法使用,用于在集合中搜索元素. 类型参数介绍: T: 要比较的对 ...
- C#语法糖之第六篇: 泛型委托- Predicate<T>、Func<T>
今天继续分享泛型委托的Predicate<T>,上篇文章讲了Action委托,这个比Action委托功不一样的地方就是委托引用方法是Bool返回值的方法,Action为无返回值.首先我们看 ...
- 游戏开发设计模式之状态模式 & 有限状态机 & c#委托事件(unity3d 示例实现)
命令模式:游戏开发设计模式之命令模式(unity3d 示例实现) 对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现) 原型模式:游戏开发设计模式之原型模式 & unity3d ...
- [六] 函数式接口的复合方法示例 predicate 谓词逻辑运算 Function接口 组合运算 比较器 逆序 比较链
复合的方法 有些函数式接口提供了允许复合的方法 也就是可以将Lambda表达式复合成为一个更加复杂的方法 之前的章节中有说到: 接口中的compose, andThen, and, or, negat ...
- jQuery之事件和批量操作、事件委托示例
一.常用事件 click(function(){...}) // 点击时触发 focus(function(){...}) // 获得焦点触发 blur(function(){...}) // 失去焦 ...
随机推荐
- HRBUST 2072:萌萌哒十五酱的礼物~(树,字典树)
题意:n个点的一棵树,树的边上有权值.一条路径的权值定义成这条路径上所有边的权值的xor.问所有路径的最大权值是多少. 思路: 首先,树上任意两点之间的路可以表示成 这两点到根节点的路- 其最近公共祖 ...
- LeetCode OJ-- Restore IP Addresses
https://oj.leetcode.com/problems/restore-ip-addresses/ string到int的ip地址格式化. 分别用 i+1,j+1,k+1,表示前三个地址段的 ...
- Spring Cloud Eureka 自我保护机制实战分析
前些天栈长在Java技术栈微信公众号分享过 Spring Cloud Eureka 的系列文章: Spring Cloud Eureka 自我保护机制 Spring Cloud Eureka 常用配置 ...
- RabbitMQ 最常用的三大模式
目录 Direct 模式 Topic 模式 Fanout 模式 Direct 模式 所有发送到 Direct Exchange 的消息被转发到 RouteKey 中指定的 Queue. Direct ...
- Extjs Ext.grid.column.Column 自适应内容
var griddb = Ext.ComponentQuery.query("Ali_DeliveryAuthorityList"); griddb = griddb[griddb ...
- hadoop之hdfs------------------FileSystem及其源码分析
FileSystem及其源码分析 FileSystem这个抽象类提供了丰富的方法用于对文件系统的操作,包括上传.下载.删除.创建等.这里多说的文件系统通常指的是HDFS(DistributedFile ...
- Spring MVC中@ControllerAdvice注解实现全局异常拦截
在网上很多都把Advice翻译成增强器,其实从翻译工具上看到,这个单词翻译是忠告,通知的意思. 首先这个注解实在Spring Web包下,而Spring MVC离不开Spring Web的依赖,所以经 ...
- IIS下安装memcached管理工具—MemAdmin
1.先看这篇文章 http://www.cnblogs.com/joylee/archive/2013/01/07/memadmin.html . 2.在IIS下安装的php-cgi.exe程序版本为 ...
- SMART OS
http://blog.csdn.net/babyfacer/article/details/8577333
- DELPHI10.2的LINUX数据库开发环境配置
DELPHI10.2的LINUX数据库开发环境配置 ubuntu使用firedac访问mysql1.安装mysql-client包sudo apt-get install mysql-client m ...