C# 委托汇总
委托汇总以及遗留问题:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleDemo.delegateUtil
{
public static class DelegateDemo
{
public delegate string Query(string para1, int para2); public delegate string Select(string p1, string p2, string p3, string p4, string p5, string p6,
string p7, string p8, string p9, string p10, string p11, string p12,
string p13, string p14, string p15, string p16, string p17, string p18); public static void Get()
{
Console.WriteLine("********************************"); {
//.net framework 1.0
Query method = new Query(QueryUser);
var value = method.Invoke("sun", );
Console.WriteLine(value);//返回用户:sun, 年龄:10
} {
//2.0
Query method = new Query(delegate (string para1, int para2)
{ return "返回用户:" + para1 + ", 年龄:" + para2;
}); string value = method.Invoke("sun", );
Console.WriteLine(value);//返回用户:sun, 年龄:20
} {
//3.0
Query method = new Query((string para1, int para2) =>
{
return "返回用户:" + para1 + ", 年龄:" + para2;
});
string value = method.Invoke("sun", );
Console.WriteLine(value);//返回用户:sun, 年龄:30
} {
//c#内置委托,带有一个string参数的,无返回值的匿名方法
Action<string> method = (string para1) =>
{
Console.WriteLine("hello " + para1 + " , age : 40");
};
method.Invoke("sun");
}
{ //Action 最多16个参数
Action<string, string, string, string, string,
string, string, string, string, string,
string, string, string, string, string,
string> method = (p1, p2, p3, p4, p5,
p6, p7, p8, p9, p0,
pq, pw, pe, pr, pp, pl) =>
{
Console.WriteLine("这里就不输出16个参数的值了");
};
method.Invoke("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
}
{ //扩展 17个参数如何设置
//自己声明一个带有17个参数的委托
Select method = (p1, p2, p3, p4, p5,
p6, p7, p8, p9, p10,
p11, p12, p13, p14, p15, p16, p17, p18) =>
{ return "这里是带有18个参数的值";
};
var value = method.Invoke("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
Console.WriteLine(value);
} {
//内置委托,有参
Func<int, string> method = (id) =>
{ return "我通过传递" + id + ",返回string的值";
};
var value = method.Invoke();
Console.WriteLine(value);
} {
//linq
User u = UserList().Find(t => t.Id == );
Console.WriteLine("linq自带的方法:" + u.Name); //自己定义一个
User tmp = UserList().FindByDemo(t => t.Id == );
Console.WriteLine("自己手写方法:" + tmp.Name); //思考 场景:查询语句会出现多条件的查询(面试中有被问到),如何做到不通过if else判断
IEnumerable<User> list = null;
List<User> datasource = UserList();
if ( == ) //假设过滤条件为用户年纪大于20
{
list = datasource.Where(t => t.Age > );
} if ( == ) //假设过滤条件为用户名字包含i的用户
{
list = datasource.Where(t => t.Name.IndexOf("i") >= );
} //以上场景写法 //合理写法: }
{
//linq to object 之IEnumerable 操作内存 使用内置委托
//linq to sql 之 IQueryable 拼接sql?? 表达式目录树,二叉树
}
Console.WriteLine("********************************");
} private static string QueryUser(string Name, int age)
{
return "返回用户:" + Name + ", 年龄:" + age;
} private static List<User> UserList()
{ List<User> tmp = new List<User>(); tmp.Add(new User() { Id = , Age = , Name = "sun" });
tmp.Add(new User() { Id = , Age = , Name = "bob" });
tmp.Add(new User() { Id = , Age = , Name = "justin" });
tmp.Add(new User() { Id = , Age = , Name = "linda" });
tmp.Add(new User() { Id = , Age = , Name = "lucy" });
tmp.Add(new User() { Id = , Age = , Name = "momo" });
tmp.Add(new User() { Id = , Age = , Name = "any" });
tmp.Add(new User() { Id = , Age = , Name = "lily" }); return tmp; } public static T FindByDemo<T>(this List<T> list, Func<T, bool> expression) where T : class, new()
{
T t = new T();
foreach (var u in list)
{
if (expression.Invoke(u))
{
t = u;
}
} return t;
} } public class User
{ public int Id { get; set; }
public int Age { get; set; }
public string Name { get; set; }
}
}
多播委托:其实也算是调用父类中的Combine和Remove方法实现,如下图所示:

上图对应结果为:
QueryUser:返回用户:sun, 年龄:88
可输出对应的string返回结果,返回结果是最后一个委托方法返回值
C# 委托汇总的更多相关文章
- [译]前端JS面试题汇总 Part 1(事件委托/this关键字/原型链/AMD与CommonJS/自执行函数)
原文:https://github.com/yangshun/front-end-interview-handbook/blob/master/questions/javascript-questio ...
- 自学MVC看这里——全网最全ASP.NET MVC 教程汇总
MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MVC技术的学习者提供一个整合学习入口.本文从 ...
- 转载:《.NET 编程结构》专题汇总(C#)
<.NET 编程结构>专题汇总(C#) - M守护神 - 博客园http://www.cnblogs.com/liusuqi/p/3213597.html 前言 掌握一门技术,首要 ...
- 收藏所用C#技术类面试、笔试题汇总
技术类面试.笔试题汇总 注:标明*的问题属于选择性掌握的内容,能掌握更好,没掌握也没关系. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补 ...
- [转]eoe社区cocos2d-x游戏引擎知识大汇总
[eoeAndroid 社区]特意为大家汇总了cocos2d-x知识贴,分量十足,纯正干或.从基础教程到游戏应用的开发,我们不让知识流失,我们要做知识的搬运工还有加工 师.希望大家能够一起的学习,和大 ...
- ASP.NET MVC 教程汇总
自学MVC看这里——全网最全ASP.NET MVC 教程汇总 MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要 ...
- 【温故而知新-万花筒】C# 异步编程 逆变 协变 委托 事件 事件参数 迭代 线程、多线程、线程池、后台线程
额基本脱离了2.0 3.5的时代了.在.net 4.0+ 时代.一切都是辣么简单! 参考文档: http://www.cnblogs.com/linzheng/archive/2012/04/11/2 ...
- 《WCF技术剖析》博文系列汇总[持续更新中]
原文:<WCF技术剖析>博文系列汇总[持续更新中] 近半年以来,一直忙于我的第一本WCF专著<WCF技术剖析(卷1)>的写作,一直无暇管理自己的Blog.在<WCF技术剖 ...
- 全网最全ASP.NET MVC 教程汇总
全网最全ASP.NET MVC 教程汇总 MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MV ...
随机推荐
- php踩过的那些坑(4) false,NULL,0,''详解
一.前方有坑 php开发过程中,难免会遇到这四个值:false,null,0,’',并且也会对这四个值进行比较,然后分别进行业务代码处理.一招不慎,就会踩到坑,影响数据判断的正确性和安全性,以至于造成 ...
- 【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程(下)
[摘要] cluster模块详解 示例代码托管在:http://www.github.com/dashnowords/blogs 阅读本章需要先阅读本系列前两章内容预热一下. 一. 引言 前两篇博文中 ...
- Eclipse 安装 ShellEd 不成功的解决办法
我是Win7, 64位的操作系统,Eclipse 版本是 32位的eclipse-java-luna-SR1-win32.在安装 ShellEd 时,一直安装不成功,前后出现两种现象: 1. 在按照网 ...
- Java中替换字符串中特定字符,replaceAll,replace,replaceFirst的区别
使用“;”替换过字符串中的“,” public class Test01 {public static void main(String[] args) {String number = " ...
- CoderForces-375D
You have a rooted tree consisting of n vertices. Each vertex of the tree has some color. We will ass ...
- POJ2528---Mayor's posters
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- 从头学pytorch(二) 自动求梯度
PyTorch提供的autograd包能够根据输⼊和前向传播过程⾃动构建计算图,并执⾏反向传播. Tensor Tensor的几个重要属性或方法 .requires_grad 设为true的话,ten ...
- SSM非springboot配置swagger2
前提:maven,ssm,不是springboot项目 1.在maven中添加依赖 <!-- Swagger2 Begin --> <!--springfox的核心jar包--> ...
- item()方法遍历字典
Python字典的遍历方法有好几种,其中一种是for...in,这个我就不说明,在Python了几乎随处都可见for...in.下面说的这种遍历方式是item()方法. item() item()方法 ...
- 发布一个基于协程和事件循环的c++网络库
目录 介绍 使用 性能 实现 日志库 协程 协程调度 定时器 Hook RPC实现 项目地址:https://github.com/gatsbyd/melon 介绍 开发服务端程序的一个基本任务是处理 ...