新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

C#匿名方法怎么用

这篇文章将为大家详细讲解有关C#匿名方法怎么用,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

创新互联公司是专业的海棠网站建设公司,海棠接单;提供网站制作、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行海棠网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

C#匿名方法

这是对变量范围的扩展。但是,下面例子说明了匿名参数还能够在它们的代码块之外执行命名方法:

privatedelegatevoidExample6();   privateint _customerId;  privatestring _customerCode;   publicint CustomerID  {  get { return _customerId; }  set { _customerId = value; }  }   publicstring CustomerCode  {  get { return _customerCode; }  set { _customerCode = value; }  }   privatevoid btnExample6_Click(object sender, EventArgs e)  {  //Populate out properties.  this.CustomerID = 90;  this.CustomerCode = "1337HK";   //Setup the delegate/anonymous method.  Example6 example =  newExample6(  delegate  {  this.ShowCustomer(this.CustomerID, this.CustomerCode);  });   //Execute the delegate.  example();   //Change the properties.  this.CustomerID = 54;  this.CustomerCode = "L4M3";   //Execute the delegate again.  // Notice that the new values are reflected.  example();  }   privatevoid ShowCustomer(int customerId, string customerCode)  {  MessageBox.Show(  String.Format("CustomerID: Customer Code: ",  customerId, customerCode));  }

要注意的是,我两次调用了与C#匿名方法相关联的委托。你可能会发现一个很有趣的事情:在这些调用中,方法会输出两组不同的值。这是因为用在C#匿名方法里的外部变量在创建C#匿名方法的时候被引用。这意味着对这些变量的任何更改都会在匿名函数访问变量的时候被反映出来。

你可能还注意到在这个实例里委托关键字后面没有括号。当C#匿名方法不需要带参数的时候,后面的括号是可选的。

关于“C#匿名方法怎么用”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


名称栏目:C#匿名方法怎么用
文章地址:http://www.wjwzjz.com/article/jjccod.html
在线咨询
服务热线
服务热线:028-86922220
TOP