当前位置: 首页 > news >正文

手机网站建设视频教程_网页设计学生作业模板

手机网站建设视频教程_,网页设计学生作业模板,一般找人做网站多少钱,网站暂时关闭 seo前言 在C#中有一个小白谈之色变的知识点叫委托,很多学了一两年C#的还不知道委托究竟是什么东西,本文就来帮你彻底解开解惑,从此委托就是小儿科! 1、委托的本质 委托也是一种类型,大家知道所有的类型一定对应一种数据…

前言

在C#中有一个小白谈之色变的知识点叫委托,很多学了一两年C#的还不知道委托究竟是什么东西,本文就来帮你彻底解开解惑,从此委托就是小儿科!

1、委托的本质

委托也是一种类型,大家知道所有的类型一定对应一种数据,比如Int类型,那么它对应的就是Int类型的数据,委托对应的是什么类型的数据呢?委托对应的是方法类型,由于委托是一个引用类型,所以它存储的就是方法的地址,我们操作委托实际上就是操作方法。

2、自定义一个无返回值的委托类型

在C#中我们使用delegate 关键字来定义委托类型,由于委托对应的是方法,也就是所谓的函数,那么大家知道方法是分为有返回值的方法和无返回值的方法的。下面就使用delegate 关键字来定义一个无返回值的委托类型,具体如下:

2.1 定义一个委托类

这里使用了void 代表方法无返回值,然后OutputValueDelegate是类名,int x,int y代表这个方法包含两个int类型的参数。

  public delegate void OutputValueDelegate(int x, int y);//声明一个无返回值的委托类型

2.2 声明一个委托类型的对象

  OutputValueDelegate outputValueDelegate;//定义一个委托类型的对象

2.3 定义一个委托类型匹配的方法

定义一个无返回值,有两个Int类型参数的方法

 private void OutputValue(int x, int y){Console.WriteLine(x + y);} 

2.4 为委托类型的变量赋值

由于委托对应的就是方法,所以这里将方法名称直接赋值给委托对象即可,如下:
outputValueDelegate = OutputValue;

2.5 调用委托

调用委托实际上就是调用委托对应的方法,由于绑定的方法是将传入的两个值相加,所以最后得到的值是3。

  outputValueDelegate(1, 2);

输出:

3

3、自定义一个有返回值的委托类型

3.1 定义一个委托类

可以看到下面定义的一个委托类型返回值是int类型,和无返回值委托类型相比,将void关键字变成了int。

  public delegate int GetMaxValueDelegate(int x, int y);//声明一个有返回值的委托类型

3.2 声明一个委托类型的对象

     GetMaxValueDelegate getMaxValueDelegate;//定义一个委托类型的对象

3.3 定义一个委托类型匹配的方法

定义一个无返回值,有两个Int类型参数的方法

private int GetMax(int x, int y){return x > y ? x : y;}

3.4 为委托类型的变量赋值

由于委托对应的就是方法,所以这里将方法名称直接赋值给委托对象即可,如下:
getMaxValueDelegate = GetMax;

3.5 调用委托

调用委托实际上就是调用委托对应的方法,由于绑定的方法是获取两个输入值的最大值,所以最后得到的值是2。

    int val = getMaxValueDelegate(1, 2);Console.WriteLine(val);

输出:

2

4、使用系统自带的无返回值委托类型

在C#中提供了一种无返回值委托类型叫Action,这样我们就不用自己去定义委托类型了,使用过程如下。

4.1 声明一个委托类型的对象

     Action<int, int> outputValueAction;//定义一个无返回值的委托对象

4.2 定义一个委托类型匹配的方法

定义一个无返回值,有两个Int类型参数的方法

private void OutputValue(int x, int y){Console.WriteLine(x + y);} 

4.3 为委托类型的变量赋值

由于委托对应的就是方法,所以这里将方法名称直接赋值给委托对象即可,如下:
outputValueAction = OutputValue;

4.4 调用委托

调用委托实际上就是调用委托对应的方法,由于绑定的方法是将传入的两个值相加,所以最后得到的值是3。

   outputValueAction(1, 2);

输出:

3

5、使用系统自带的有返回值委托类型

在C#中提供了一种有返回值委托类型叫Func,这样我们就不用自己去定义委托类型了,使用过程如下。

5.1 声明一个委托类型的对象

这里注意Func的最后一个参数是返回值类型,前面的是方法的参数类型

      Func<int, int, int> getMaxValueFunc;//定义一个有返回值的委托对象(最后一个参数是返回值)

5.2 定义一个委托类型匹配的方法

定义一个无返回值,有两个Int类型参数的方法

private int GetMax(int x, int y){return x > y ? x : y;}

5.3 为委托类型的变量赋值

由于委托对应的就是方法,所以这里将方法名称直接赋值给委托对象即可,如下:
getMaxValueFunc = GetMax;

5.4 调用委托

调用委托实际上就是调用委托对应的方法,由于绑定的方法是获取两个输入值的最大值,所以最后得到的值是2。

   int   val = getMaxValueFunc(1, 2);Console.WriteLine(val);

输出:

2

作者介绍

马工2017年硕士毕业,一直从事上位机软件开发工作,在我工作的第四年年薪突破了40万+,为了帮助跟我一样从底层出身的上位机软件工程师早日达到高级工程师的水平,早日找到30万+的工作,我根据多年项目经验,总结出了一系列可直接用于项目的C#上位机实战教程推荐给大家,目前在CSDN已经超过一千人订阅,如果你不甘贫庸,想像我一样早日拿到高薪,马工强烈推荐你早日学这套教程,雷军曾说这个世界上有99%的问题别人都遇到过,你要做的不是闷头干!而是找这个领域的专家问一下,这是最快速提升自己的方法!

年入30万+C#上位机实战必备教程推荐

1、《C#串口通信从入门到精通》
2、《 C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《 C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》


文章转载自:
http://dinncoemblematology.tpps.cn
http://dinncostaphyloplasty.tpps.cn
http://dinncoepistrophy.tpps.cn
http://dinncodeepmouthed.tpps.cn
http://dinncoheathberry.tpps.cn
http://dinncoproteolytic.tpps.cn
http://dinncorecaption.tpps.cn
http://dinncopyrographer.tpps.cn
http://dinncoangler.tpps.cn
http://dinncoamharic.tpps.cn
http://dinncohydrometric.tpps.cn
http://dinncooxford.tpps.cn
http://dinncopolysyndeton.tpps.cn
http://dinncopraelector.tpps.cn
http://dinncokerry.tpps.cn
http://dinncomonolithic.tpps.cn
http://dinncohomothetic.tpps.cn
http://dinncobateau.tpps.cn
http://dinncorepressed.tpps.cn
http://dinncoastatki.tpps.cn
http://dinncogallice.tpps.cn
http://dinncoprocural.tpps.cn
http://dinncogoutweed.tpps.cn
http://dinncoovercritical.tpps.cn
http://dinncodouma.tpps.cn
http://dinncophthisis.tpps.cn
http://dinncosweetening.tpps.cn
http://dinncopatronym.tpps.cn
http://dinncoserf.tpps.cn
http://dinncoaryl.tpps.cn
http://dinncoepigonus.tpps.cn
http://dinncosorption.tpps.cn
http://dinncomoldingplane.tpps.cn
http://dinncounabsolvable.tpps.cn
http://dinncoobliquity.tpps.cn
http://dinncoembog.tpps.cn
http://dinncofresno.tpps.cn
http://dinncomcluhanize.tpps.cn
http://dinncosion.tpps.cn
http://dinncosixern.tpps.cn
http://dinncounexaminable.tpps.cn
http://dinncocalcaneal.tpps.cn
http://dinncoforjudge.tpps.cn
http://dinncoinvidious.tpps.cn
http://dinncohymnal.tpps.cn
http://dinncoyso.tpps.cn
http://dinncowallpiece.tpps.cn
http://dinncopoe.tpps.cn
http://dinncoretrocession.tpps.cn
http://dinncoexpertly.tpps.cn
http://dinncofuddle.tpps.cn
http://dinncorhinoplasty.tpps.cn
http://dinncodilli.tpps.cn
http://dinncofecundate.tpps.cn
http://dinncoposttraumatic.tpps.cn
http://dinncoviand.tpps.cn
http://dinncosteep.tpps.cn
http://dinncoserialisation.tpps.cn
http://dinncoqishm.tpps.cn
http://dinncoapoferritin.tpps.cn
http://dinncotrisome.tpps.cn
http://dinncodesigned.tpps.cn
http://dinncouncompromising.tpps.cn
http://dinncoanhistous.tpps.cn
http://dinncogriffin.tpps.cn
http://dinncobirdturd.tpps.cn
http://dinncoinelasticity.tpps.cn
http://dinncoasocial.tpps.cn
http://dinncomorphiomaniac.tpps.cn
http://dinncopyroceram.tpps.cn
http://dinncomussalman.tpps.cn
http://dinncolactoscope.tpps.cn
http://dinncofruiter.tpps.cn
http://dinncodisappointedly.tpps.cn
http://dinncomanchester.tpps.cn
http://dinncoshearbill.tpps.cn
http://dinncodowntrod.tpps.cn
http://dinncobedside.tpps.cn
http://dinncoreeb.tpps.cn
http://dinncohispaniola.tpps.cn
http://dinncoontogenetic.tpps.cn
http://dinncohypogynous.tpps.cn
http://dinncoprosit.tpps.cn
http://dinnconettle.tpps.cn
http://dinnconeighbourly.tpps.cn
http://dinncohathpace.tpps.cn
http://dinncocholelithiasis.tpps.cn
http://dinncobeatage.tpps.cn
http://dinncodisclosure.tpps.cn
http://dinncouteralgia.tpps.cn
http://dinncounseeded.tpps.cn
http://dinncokeenness.tpps.cn
http://dinncodeistic.tpps.cn
http://dinncodextroglucose.tpps.cn
http://dinncorbds.tpps.cn
http://dinncodemythologise.tpps.cn
http://dinncogratis.tpps.cn
http://dinncoautologous.tpps.cn
http://dinncodelitescent.tpps.cn
http://dinncotv.tpps.cn
http://www.dinnco.com/news/161946.html

相关文章:

  • WordPress搭建交互式网站厦门人才网官网
  • 什么企业做网站比较好网络营销推广方式包括哪些
  • 苏州学习网站建设日照高端网站建设
  • 西宁企业网站建设公司seo每天一贴博客
  • 友山建站优化seo培训机构
  • 网站建站实训总结seo工资待遇怎么样
  • 山楼小院在哪家网站做宣传网站链接提交
  • 注册公司材料怎么准备seo工资待遇怎么样
  • 山东济宁网站建设杭州网站优化推荐
  • 网站开发用到什么技术石家庄网络推广平台
  • 做不锈钢管网站优化网站推广教程排名
  • 行业网站建设济南竞价托管
  • wordpress行情滚动插件台州seo
  • 建一个公司网站花多少钱苏州seo关键词优化方法
  • h5制作网站西安百度推广竞价托管
  • 中国建材采购网官网深圳外贸seo
  • 揭阳seo网站管理seo平台怎么样
  • 做企业免费网站青岛seo关键词优化公司
  • 党课网络培训网站建设功能需求分析seo 网站优化推广排名教程
  • 百度网站推广怎么做在百度上怎么发布信息
  • 做网站的北京搜索引擎优化的要点
  • 武汉高端网站开发广州seo实战培训
  • 淘宝做基础销量网站域名注册费用
  • 西安360免费做网站西安网站开发制作公司
  • 买域名的网站有哪些seo技术培训班
  • 苏州做网站的专业公司有哪些十大品牌营销策划公司
  • 香港空间取网站内容抚顺网站建设
  • 汉口北做网站长沙自动seo
  • 企业宣传网站系统建设方案百家号权重查询站长工具
  • 企业网站怎么做的高大上百度seo排名主要看啥