C#语言还是比较常见的东西,这里我们主要介绍C#匿名方法,包括介绍当C#匿名方法不需要带参数的时候,后面的括号是可选的等方面。

乌海网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。成都创新互联从2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联。
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#匿名方法详细剖析
                
                文章分享:http://www.csdahua.cn/qtweb/news34/496634.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网