.Net 委托 delegate 学习
{
Console.WriteLine("DoSome");
}
{
return num * num;
}
doSome += new Action(DoSome);
doSome += DoSome;
public class Student
{
public int Id { get; set; } public string Name { get; set; } public int ClassId { get; set; } public int Age { get; set; }
} public static class StudentManager
{
public static List<Student> students = new List<Student>()
{
new Student(){ Id=,Name="张三",ClassId=,Age= },
new Student(){ Id=,Name="李四",ClassId=,Age= },
new Student(){ Id=,Name="王五",ClassId=,Age= },
new Student(){ Id=,Name="赵六",ClassId=,Age= },
new Student(){ Id=,Name="杨幂",ClassId=,Age= },
new Student(){ Id=,Name="范冰冰",ClassId=,Age= },
new Student(){ Id=,Name="张学友",ClassId=,Age=},
new Student(){ Id=,Name="张三1",ClassId=,Age= },
new Student(){ Id=,Name="张三2",ClassId=,Age= },
new Student(){ Id=,Name="张三3",ClassId=,Age= },
new Student(){ Id=,Name="张三4",ClassId=,Age= },
new Student(){ Id=,Name="张三5",ClassId=,Age= },
new Student(){ Id=,Name="张三6",ClassId=,Age= },
new Student(){ Id=,Name="张三7",ClassId=,Age= },
new Student(){ Id=,Name="张三8",ClassId=,Age= },
new Student(){ Id=,Name="张三9",ClassId=,Age= },
new Student(){ Id=,Name="张三0",ClassId=,Age= },
new Student(){ Id=,Name="张三11",ClassId=,Age= },
new Student(){ Id=,Name="张三a",ClassId=,Age= },
new Student(){ Id=,Name="张三b",ClassId=,Age= },
new Student(){ Id=,Name="张三c",ClassId=,Age= },
new Student(){ Id=,Name="张三d",ClassId=,Age= },
new Student(){ Id=,Name="张三e",ClassId=,Age= },
new Student(){ Id=,Name="张三f",ClassId=,Age= },
new Student(){ Id=,Name="张三g",ClassId=,Age= },
new Student(){ Id=,Name="张三h",ClassId=,Age= },
new Student(){ Id=,Name="张三i",ClassId=,Age= },
new Student(){ Id=,Name="张三j",ClassId=,Age= },
new Student(){ Id=,Name="张三k",ClassId=,Age= },
}; public static List<Student> FindStudents(Func<Student,bool> func)
{
List<Student> stus = new List<Student>(); foreach (var item in students)
{
if (func(item))
{
stus.Add(item);
}
}
return stus;
} /// <summary>
/// 查找ClassId为3001的学生
/// </summary>
/// <param name="student">学生</param>
/// <returns>是否为3001班级的学生</returns>
public static bool GetClassId(Student student)
{
if (student.ClassId==)
{
return true;
} return false; }
/// <summary>
/// 年龄大于20的学生
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static bool GetBigAge(Student student)
{
if (student.Age>)
{
return true;
}
return false;
}
/// <summary>
/// 年龄大于15 并且ClassId为1021
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
public static bool GetStuByClassIdAndAge(Student student)
{
if (student.Age > && student.ClassId==)
{
return true;
}
return false;
} }
下面这个是在Main方法中执行查询学生
//List<Student> stus = StudentManager.students;
//Console.WriteLine("姓名---年龄---班级--编号");
//foreach (var item in stus)
//{
// Console.WriteLine(item.Name+"---"+item.Age+"---"+item.ClassId+"---"+item.Id);
//}
List<Student> stus1= StudentManager.FindStudents(StudentManager.GetStuByClassIdAndAge);
Console.WriteLine("姓名---年龄---班级--编号");
foreach (var item in stus1)
{
Console.WriteLine(item.Name + "---" + item.Age + "---" + item.ClassId + "---" + item.Id);
}
.Net 委托 delegate 学习的更多相关文章
- IOS开发使用委托delegate在不同窗口之间传递数据
IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容.在IOS开发里两个UIView窗口之间 ...
- [.NET] C# 知识回顾 - 委托 delegate (续)
C# 知识回顾 - 委托 delegate (续) [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6046171.html 序 上篇<C# 知识回 ...
- [C#] C# 知识回顾 - 委托 delegate
C# 知识回顾 - 委托 delegate [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/6031892.html 目录 What's 委托 委托的属性 ...
- C# 委托Delegate(一) 基础介绍&用法
本文是根据书本&网络 前人总结的. 1. 前言 定义&介绍: 委托Delegate是一个类,定义了方法的类型, 使得可以将方法当做另一个方法的参数来进行传递,这种将方法动态地赋给参数的 ...
- 为什么不能把委托(delegate)放在一个接口(interface)当中?
stackoverflow上有人问,为什么不能把委托放在一个接口当中? 投票最多的第一个答案第一句话说,“A Delegate is just another type, so you don't g ...
- C# 代理/委托 Delegate
本文转载自努力,努力,努力 1. 委托的定义:委托是函数的封装,它代表一"类"函数.他们都符合一定的签名:拥有相同的参数列表,返回值类型.同时,委托也可以看成是对函数的抽象,是函数 ...
- c# 委托 delegate
委托是一种存储函数引用的类型,在事件和事件的处理时有重要的用途 通俗的说,委托是一个可以引用方法的类型,当创建一个委托,也就创建一个引用方法的变量,进而就可以调用那个方法,即委托可以调用它所指的方法. ...
- 理解委托(delegate)及为什么要使用委托
理解委托(delegate)及为什么要使用委托 委托:是一种定义方法签名的类型. 当实例化委托时,您可以将其实例与任何具有兼容签名的方法相关联. 您可以通过委托实例调用方法. 上述为官方说法,理解起来 ...
- 深入理解委托(Delegate)
前言 委托其实一直以来都感觉自己应该挺熟悉的,直到最近又去翻了翻 CLR via C#,感觉我之前的理解可能还有失偏颇.在这记录一下. 之前文章的链接: 接口和委托的泛型可变性 C#高级编程笔记 De ...
随机推荐
- SpringBoot(三)_controller的使用
针对controller 中 如何使用注解进行解析 @RestController 返回数据类型为 Json 字符串,特别适合我们给其他系统提供接口时使用. @RequestMapping (1) 不 ...
- 神奇的Scala Macro之旅(四)- BeanBuilder
在Java开发中,经常会有一个需求,将一个 Bean 复制到另外一个 Bean,尤其是在后台分层的场景下,在不同的层之间传递信息,经常需要进行 这样的一个对象复制工作,类似于: val source: ...
- with管理文件操作上下文
目录 with管理文件操作上下文(掌握) with管理文件操作上下文(掌握) 之前我们使用open()方法操作文件,但是open打开文件后我们还需要手动释放文件对操作系统的占用.但是其实我们可以更方便 ...
- Java工程师必备书单
微信公众号[程序员江湖] 作者黄小斜,斜杠青年,某985硕士,阿里 Java 研发工程师,于 2018 年秋招拿到 BAT 头条.网易.滴滴等 8 个大厂 offer,目前致力于分享这几年的学习经验. ...
- 【转】javascript笔记之apply、call、bind用法
原文地址:https://www.cnblogs.com/coco1s/p/4833199.html apply.call 在 javascript 中,call 和 apply 都是为了改变某个函数 ...
- .net之设计模式
在上一篇文章里我通过具体场景总结了“.net面向对象的设计原则”,其中也多次提到一些设计模式方面的技术,可想而知,设计模式在我们的开发过程中也是必不可少的.今天我们就来简单交流下设计模式.对于设计模式 ...
- 设计模式之行为类模式PK
行为类模式包括: 责任链模式 命令模式 解释器模式 迭代器模式 中介者模式 备忘录模式 观察者模式 状态模式 策略模式 模板方法模式 访问者模式 行为型模式涉及到算法和对象间职责的分配 行为类模式关注 ...
- openlayers一:显示地图与鼠标地理坐标
openlayers两个好用的开源JS互动地图库之一,另一个是leaflet. openlayers的特点是是大而全,自身包含绝大多数功能,文档好看. leaflet是小而美,自身小,但支持扩展,好用 ...
- C语言中的位段(位域)知识
在结构体或类中,为了节省成员的存储空间,可以定义某些由位组成的字段,这些字段可以不需要以byte为单位. 这些不同位长度的字段实际存储于一个或多个整形变量.位段成员必须声明为int, signed i ...
- CF914G Sum the Fibonacci FWT、子集卷积
传送门 一道良心的练习FWT和子集卷积的板子-- 具体来说就是先把所有满足\(s_a \& s_b = 0\)的\(s_a \mid s_b\)的值用子集卷积算出来,将所有\(s_a \opl ...