自定义委托

//委托方法

  public static int Add(int a, int b){return a + b;}

//委托方法的定义

 delegate int AddDelegate(int a, int b);

//委托初始化(方法名)

  AddDelegate addDelegate = new AddDelegate(Add);

//委托的调用(方法参数)

 int c = AddDelegate.Invoke(3, 4);

代码

using System;
using System.Collections;
using System.Collections.Generic;
​
namespace ConsoleApp2
{
​class Program{
​class Class1{static void Main(string[] args){AddDelegate AddDelegate = new AddDelegate(Add);int c = AddDelegate.Invoke(3, 4);Console.WriteLine(c);}
​public static int Add(int a, int b){int c = a + b;return c;}}
​delegate int AddDelegate(int a, int b);}
}
​

泛型委托

解决代码重用,写出通用委托

//泛型委托方法

 public static void WXKINT(int a ,int b){
​int c = a + b;Console.WriteLine(c);}
​public static void  WXKDOUBLE(double a, double b){double c = a + b;Console.WriteLine(c);}

//泛型委托的定义

 delegate void WXKDelegate<T>(T a, T b);

//委托初始化(方法名)

 WXKDelegate <int> wXKDelegate = new WXKDelegate<int>(WXKINT);

//委托的调用(方法参数)

      wXKDelegate(2, 5);//调运委托可以不加Invoke

代码

using System;
using System.Collections;
using System.Collections.Generic;
​
namespace ConsoleApp2
{
​class Program{
​class Class1{static void Main(string[] args){
​WXKDelegate <int> wXKDelegate = new WXKDelegate<int>(WXKINT);wXKDelegate(2, 5);//调运委托可以不加Invoke}
​
​public static void WXKINT(int a ,int b){
​int c = a + b;Console.WriteLine(c);}
​public static void  WXKDOUBLE(double a, double b){double c = a + b;Console.WriteLine(c);}}delegate void WXKDelegate<T>(T a, T b);}
}

预定义委托

不需要定义委托

//委托方法

  public static void Show(string a ,int num){
​Console.WriteLine(a);}
​public static void  SHOW2(int a,int b,int c){Console.WriteLine(c);}
​public static string SHOW3(){return "有返回值";}
public static int SHOW4(int a){return a;}

//委托不需定义

//委托初始化和调用(方法名)

                 //无返回值Action<string, int> action = new Action<string, int>(Show);action("无返回值",1);
​
​Action<int, int,int> action2 = new Action<int,int,int>(SHOW2);action2(1,2,3);
​//有返回值Func<string> func1 = new Func<string>(SHOW3);Console.WriteLine(func1());
​Func<int, int> func2 = new Func<int, int>(SHOW4);Console.WriteLine(func2(111));

代码

using System;
using System.Collections;
using System.Collections.Generic;
​
namespace ConsoleApp2
{
​class Program{
​class Class1{static void Main(string[] args){//无返回值Action<string, int> action = new Action<string, int>(Show);action("无返回值",1);
​
​Action<int, int,int> action2 = new Action<int,int,int>(SHOW2);action2(1,2,3);
​//有返回值Func<string> func1 = new Func<string>(SHOW3);Console.WriteLine(func1());
​Func<int, int> func2 = new Func<int, int>(SHOW4);Console.WriteLine(func2(111));Console.ReadKey();}
​public static string SHOW3(){return "有返回值";}public static int SHOW4(int a){return a;}
​public static void Show(string a ,int num){
​Console.WriteLine(a);}
​public static void  SHOW2(int a,int b,int c){Console.WriteLine(c);}}}
}
​

lambda

本质是匿名方法 ,让我们更快更简单完成委托,还可以让实例化委托的方法访问局部变量

起初代码是这样

 //最初是这种复杂分开写Action<string> action = new Action<string>(Show);action("我是泛型委托Action");
//还要在下面写委托方法
​public static void Show(string a){
​Console.WriteLine(a);}
​

第一步演变

  //使用匿名方法Action<string> action2 = new Action<string>(delegate (string msg){Console.WriteLine(msg);});action2("我是委托action,使用了匿名方法");

接下来更简单一下

​//=>念成gose toAction<string> action3 = new Action<string>( (string msg)=>{Console.WriteLine(msg);});action3("我是委托action,使用了匿名方法");

更简单的写下来

  //一个参数 一个语句的方法Action<string> action4 = new Action<string>((msg) => Console.WriteLine(msg));action4("我是委托action,使用了匿名方法");

接下来就是lambda

 //一个参数 一个语句的方法Action action5 = new Action(() => Console.WriteLine("无返回值,无参数"));action5();
   //有返回值,单条语句Func<string> func6 = new Func<string>(() => "有返回值,单条语句");func6();
   //有返回值,多条语句Func<string> func7 = new Func<string>(() =>{Console.WriteLine("有返回值,多条语句");Console.WriteLine("有返回值,多条语句");Console.WriteLine("有返回值,多条语句");Console.WriteLine("有返回值,多条语句");return "有返回值,多条语句";});func7();

代码:

using System;
using System.Collections;
using System.Collections.Generic;
​
namespace ConsoleApp2
{
​class Program{
​class Class1{static void Main(string[] args){//最初是这种复杂分开写Action<string> action = new Action<string>(Show);action("我是泛型委托Action");
​//使用匿名方法Action<string> action2 = new Action<string>(delegate (string msg){Console.WriteLine(msg);});action2("我是委托action,使用了匿名方法");
​//=>念成gose toAction<string> action3 = new Action<string>( (string msg)=>{Console.WriteLine(msg);});action3("我是委托action,使用了匿名方法");
​//一个参数 一个语句的方法Action<string> action4 = new Action<string>((msg) => Console.WriteLine(msg));action4("我是委托action,使用了匿名方法");
​//无返回值 无参数//一个参数 一个语句的方法Action action5 = new Action(() => Console.WriteLine("无返回值,无参数"));action5();//有返回值,单条语句Func<string> func6 = new Func<string>(() => "有返回值,单条语句");func6();//有返回值,多条语句Func<string> func7 = new Func<string>(() =>{Console.WriteLine("有返回值,多条语句");Console.WriteLine("有返回值,多条语句");Console.WriteLine("有返回值,多条语句");Console.WriteLine("有返回值,多条语句");return "有返回值,多条语句";});func7();}
​public static void Show(string a){
​Console.WriteLine(a);}
​}}
}
​

C#进阶语法-委托和Lambda相关推荐

  1. C# 委托 (一)—— 委托、 泛型委托与Lambda表达式

    C# 委托 (一)-- 委托. 泛型委托与Lambda表达式 2018年08月19日 20:46:47 wnvalentin 阅读数 2992 版权声明:此文乃博主之原创.鄙人才疏,望大侠斧正.此文可 ...

  2. 委托、Lambda表达式和事件

    1. 引用方法 委托是寻址方法的.NET版本.在C++中,函数指针只不过是一个指向内存位置的指针,它不是类型安全的.我们无法判断这个指针实际指向什么,像参数和返回类型等项就更无从知晓了.而.NET委托 ...

  3. java8 ie_Java8语法糖之Lambda表达式_Hudie.的博客-CSDN博客

    原文作者:编程一只蝶 原文标题:Java8语法糖之Lambda表达式 发布时间:2021-02-16 13:08:40 一.Lambda表达式简介 Lambda表达式,是Java8的一个新特性,也是J ...

  4. [深入学习C#]匿名函数、委托和Lambda表达式

    转载自诗人江湖老,原文地址 匿名函数(Anonymous Function)是表示"内联"方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树 ...

  5. part01.03 委托与 Lambda 表达式(三):Lambda 表达式

    "Lambda 表达式"是一个匿名函数,它可以包含表达式和语句,用于创建委托或表达式树类型 A. 用Lambda表达式代替匿名方法,复杂冗长的形式 格式:( 显式类型参数列表 )= ...

  6. 02_python进阶语法

    python进阶语法 一.正则表达式与json 正则表达式 正则表达式是一个特殊的字符序列,用于判断一个字符串是否与我们所设定的字符序列匹配 可用于快速检索文本,替换文本 元字符与普通字符 a = ' ...

  7. C#委托与Lambda表达式

    C#委托与Lambda表达式 一.自定义委托 总结 1.委托必须先声明在使用: 2.委托使用必须实例化,在实例化后要把委托的方法名带入: 3.委托的调用必须与委托的方法参数一致: 二.泛型与多播委托 ...

  8. 【学习笔记】JS进阶语法一事件进阶

    内容整理自<从0到1Javascript快速上手>下半部分-进阶语法篇 示例:event对象keyCode属性获取键盘上下左右键 <!DOCTYPE html> <htm ...

  9. 【学习笔记】JS进阶语法一事件基础

    内容整理自<从0到1Javascript快速上手>下半部分-进阶语法篇  示例:键盘松开一瞬间触发的事件 <!DOCTYPE html> <html><hea ...

最新文章

  1. Code Generate of Power Designer[转]
  2. 【时间序列】时序预测竞赛之异常检测算法综述
  3. Java字符流的使用
  4. mysql20170410练习代码+笔记
  5. (102)FPGA面试题-如何选择FPGA型号?
  6. 2020 各大厂分享ppt
  7. sap 打印预览界面点击打印时记录打印次数_9个Excel打印神技巧!从此打印不求人!...
  8. AVG游戏《裂缝》策划案
  9. HDU 4699 题解
  10. WhatsApp创始人:从领救济到身价68亿
  11. Matlab中ismissing函数的使用
  12. 高并发高负载网站系统架构
  13. 《中国历代著名文学家评传》目录
  14. 全国应用计算机水平考试,全国计算机应用水平考试
  15. 基于vc的数字图像分割——基于阙值的分割方法
  16. 参加华为HCIP的培训班吗?
  17. 【黄啊码】vue-pdf预览时无法显示印章和中文字体或者乱码(简单粗暴)
  18. DIV背景半透明 样式
  19. unity-shader-ShaderGraph可视化shader
  20. Caffe和caffe2漫谈

热门文章

  1. 蛙蛙推荐:蛙蛙牌无组件上传类
  2. SAP,ABAP自开发批导程序批量维护ME11、ME12信息记录,涉及表EINA、EINE
  3. 1 项目总结(编程项目+ 实验室项目)
  4. excel vba根据单元格改变时计算其它单元
  5. 稳定性测试-模拟网络异常
  6. 【EmbeddedDev】BBBRTL8188CUS搭建wifi AP 问题整理
  7. cad菜单栏快捷键_天正CAD界面“菜单栏”不见了怎么办?教你3招秒解决,实用神技巧...
  8. 小米菲的Tableau学习日记02:Tableau的工作区介绍--什么是工作表、仪表板和故事?
  9. Ubuntu系统下安装eclipse
  10. 多任务进化优化算法(三)利用显式自编码器的进化多任务、基于生物群落共生的进化多任务优化简介