1.了解委托

  MyDelegate类代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyDelegate
{
/// <summary>
/// 委托可以定义在类外面
/// </summary>
public delegate void OutNoReturnNoPara();
public delegate void OutNoReturnWithPara(int x, int y); class DelegateClass
{
/// <summary>
/// 1.声明委托,委托的参数与函数的参数必须一致
/// </summary>
public delegate void NoReturnNoPara();
public delegate void NoReturnWithPara(int x, int y);
public delegate string NoPara();
public delegate DateTime WithPara(string name,int size); public static void Show()//静态方法的委托只能调用静态方法
{
//2.实例化委托,这里的method实例化后就是一个Plus函数
NoReturnWithPara method = new NoReturnWithPara(Plus);//等价于NoReturnWithPara method = Plus;
//3.调用委托
method.Invoke(, );//等价于method(3, 4);
method.BeginInvoke(, ,null,null);//补充:新建一个线程,异步调用 }
public static void Plus(int x,int y)
{
Console.WriteLine("这里是Plus x={0} y={1}", x, y);
}
}
}

  在Program使用DelegateClass.Show();

  可以调用Plus这个方法

2.委托的用处

  1)打招呼===》普通方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyDelegate
{
class GreetingClass
{public static void Greeting(string name,PeopleType type)//输入和谁打招呼,和这个人是哪个国家的人
{
if(type==PeopleType.Chinese)
{
Console.WriteLine("{0}早上好", name);
}
else if(type==PeopleType.English)
{
Console.WriteLine("{0}Morning", name);
}
else
{
throw new Exception("wrong PeopleType");
}
}
}public enum PeopleType //枚举,定义国家
{
Chinese,English
}
}

  在Program使用GreetingClass.Greeting("kxy",PeopleType.Chinese);//kxy是一个中国人,所以使用PeopleType.Chinese

可以实现给不同国家的人打招呼用对应国家的语言

  但是如果我们需要增加一种语言,则需要修改枚举PeopleType和函数Greeting

  2)打招呼===》委托方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyDelegate
{
class GreetingClass
{ public static void GreetingChinese(string name)
{
Console.WriteLine("{0}早上好", name);
}
public static void GreetingEnglish(string name)
{
Console.WriteLine("{0}Morning", name);
}
}
public delegate void GreetingHandler(string name);
}

  Program代码如下:

 GreetingHandler handle = new GreetingHandler(GreetingClass.GreetingEnglish);//指明是哪国人
handle("flt");//输入人的名字

  当需要增加一种新的语言时,直接增加一个Greeting*******函数就可以了,解除耦合

3.lambda的演化

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyDelegate
{
public class Student
{
public delegate void NoReturnWithPara(string name, DateTime now);
/// <summary>
/// 匿名函数向lambda演化的过程
/// </summary>
public static void show()
{
//普通委托
NoReturnWithPara method = new NoReturnWithPara(Study); //使用匿名函数
NoReturnWithPara method1 = new NoReturnWithPara(
delegate (string name, DateTime now)
{
Console.WriteLine("{0} {1}在学习", name, now);
}); //匿名函数向lambda演化,去掉delegate,加入=>
NoReturnWithPara method2 = new NoReturnWithPara(
(string name, DateTime now)=>
{
Console.WriteLine("{0} {1}在学习", name, now);
}); //匿名函数向lambda演化,去掉类型,因为委托函数会自动标识类型
NoReturnWithPara method3 = new NoReturnWithPara(
(name, now) =>
{
Console.WriteLine("{0} {1}在学习", name, now);
}); //匿名函数向lambda演化,去掉new,这也是最终形式
NoReturnWithPara method4 = (name, now) =>
{
Console.WriteLine("{0} {1}在学习", name, now);
}; //匿名函数向lambda演化,如果方法体只有一行,可以去掉大括号
NoReturnWithPara method10 = (name, now) => Console.WriteLine("{0} {1}在学习", name, now);
method10("zjx",DateTime.Now);
} public static void Study(string name, DateTime now)
{
Console.WriteLine("{0} {1}在学习",name,now);
}
}
}

4.其他多种委托使用lambda

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace MyDelegate
{
public class Student
{
public delegate void NoReturnWithPara(string name, DateTime now);
public delegate int WithReturnWithPara(int x,int y);
public delegate string WithReturnNoPara();
/// <summary>
/// 委托向lambda演化的过程
/// </summary>
public static void show()
{//没有参数,返回一个string类似,可以省略return和大括号
WithReturnNoPara meth = () => "我爱学习"; //有两个int参数,返回int类型
WithReturnWithPara meth2 = (x, y) => x + y; ///////////////////////////在开发中基本不需要自己定义委托,可以使用下面方法/////////////////////////////////
//Action是一个委托,无返回值 <定义参数类型>
Action action1 = () => { };//无参数无返回值
Action<string> action2 = s =>{ };//有参数无返回值,且只有一个参数时可以去掉参数的小括号
Action<int, string, DateTime> action3 = (x, y, z) => { };//可以有多个参数,自定义 //Func是一个委托,有返回值 <定义参数类型,和返回值类型>
Func<string> func1 = () => "我爱学习"; //<只有一个类型>,该类型是指返回值类型
Func<string, int, DateTime> func2 = (x, y) => DateTime.Now;//<参数类型,参数类型,返回值类型>
}
}
}

5.lambda向linq的扩展

public static void LinqShow()
{
//-----------------------定义一个1到99的数组-------------------------
List<int> intlist = new List<int>();
for(int i=;i<;i++)
{
intlist.Add(i);
}
//-----------------------打印大于55的数字----------------------------
//-----------------------传统方法-----------------------------------
foreach(int i in intlist)
{
if(i>)
{
Console.WriteLine(i);
}
}
//-----------------------linq方法-----------------------------------
var linq = from s in intlist
where s >
select s;
foreach(int i in linq)
{
Console.WriteLine(i);
}
//-----------------------linq方法扩展为lambda------------------------
foreach (int i in intlist.Where<int>(m => m > ))
{
Console.WriteLine(i);
}
//上面foreach可详写为:
foreach (int i in intlist.Where<int>(//Where是Linq下的一个Func委托,有返回值
(m) => { return m > ; }//匿名函数,使用lambda表达式
)
)
{
Console.WriteLine(i);
}
}

委托(作用:解耦),lambda的演化的更多相关文章

  1. C++ lambda的演化

    翻译自https://www.bfilipek.com/2019/02/lambdas-story-part1.html与https://www.bfilipek.com/2019/02/lambda ...

  2. C++实现委托机制(三)——lambda表达式封装

    C++.引言:              其实原本没打算写这一章的,不过最后想了想,嗯还是把lambda表达式也一并封装进去,让这个委托也适应lambda表达式的注册.不过在之前还是需要先了解lamb ...

  3. C#多线程+委托+匿名方法+Lambda表达式

    线程 下面是百度写的: 定义英文:Thread每个正在系统上运行的程序都是一个进程.每个进程包含一到多个线程.进程也可能是整个程序或者是部分程序的动态执行.线程是一组指令的集合,或者是程序的特殊段,它 ...

  4. C#委托多播、Lambda表达、多线程、任务

    class Program { static void Main(string[] args) { Action<double> ops = MathOperations.Mutiply; ...

  5. 委托-异步调用-泛型委托-匿名方法-Lambda表达式-事件【转】

    1. 委托 From: http://www.cnblogs.com/daxnet/archive/2008/11/08/1687014.html 类是对象的抽象,而委托则可以看成是函数的抽象.一个委 ...

  6. (28)C#委托,匿名函数,lambda表达式,事件

    一.委托 委托是一种用于封装命名和匿名方法的引用类型. 把方法当参数,传给另一个方法(这么说好理解,但实际上方法不能当参数,传入的是委托类型),委托是一种引用类型,委托里包含很多方法的引用 创建的方法 ...

  7. 委托初级篇——lambda表达式的推导

    public delegate void ConsoleWriteStr(string name,DateTime now); public delegate int DelegateAdd(int ...

  8. 委托、回调 Lambda表达式书写方式

  9. .NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式

    开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Func/Predicate)和超爱的Lambda表达式.为了方便码农们,. ...

随机推荐

  1. 关于form与表单操作

    form表单自动提交规则 form表单中只有一个type=text的input,在input中按enter键,会自动提交: form表单中有多个type=text的input,且无type=submi ...

  2. Nginx上部署HTTPS + HTTP2

    Nginx上部署HTTPS依赖OpenSSL库和包含文件,即须先安装好libssl-dev(或者OpenSSL),且ln -s /usr/lib/x86_64-linux-gnu/libssl.so ...

  3. 自定义QMenu

    参考: http://blog.csdn.net/qq1623803207/article/details/77449884 http://blog.sina.com.cn/s/blog_a6fb6c ...

  4. apache2 以及https证书配置

    环境Ubuntu12.04 server 配置 1,首先在进入找到/etc/apache2/apache2.conf的配置文件,里面有包含了较多配置文件的路径如:httpd.conf/ports.co ...

  5. (排序的新方法)nyoj1080-年龄排序

    1080-年龄排序 内存限制:234MB 时间限制:2000ms 特判: No通过数:148 提交数:575 难度:0 题目描述: JXB经常向HJS炫耀他们家乡那里有多么多么好,但是HJS大牛从来对 ...

  6. Linux中rpm的使用

    1.安装 rpm -i 需要安装的包文件名 举例如下: rpm -i example.rpm 安装 example.rpm 包: rpm -iv example.rpm 安装 example.rpm ...

  7. IDEA常用快捷键整理(Mac OS X版本)

    最近eclipse把我弄疯了!各种提示没有!烦,果断用了IDEA. 一.前提 IDEA版本:IntelliJ IDEA 15.0.3 Keymaps:Mac OS X 二.视图切换快捷键 1.cmd ...

  8. spring boot 返回json字符串 null值转空字符串

    @Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.cl ...

  9. OS + linux proxy

    s Linux主机通过代理服务器进行网络连接 http://www.linuxidc.com/Linux/2015-01/111703.htm 新手用Linux做代理服务器 三招搞定 http://b ...

  10. Tensorflow做阅读理解与完形填空

    catalogue . 前言 . 使用的数据集 . 数据预处理 . 训练 . 测试模型运行结果: 进行实际完形填空 0. 前言 开始写这篇文章的时候是晚上12点,突然想到几点新的理解,赶紧记下来.我们 ...