`
hunxiejun
  • 浏览: 1145355 次
文章分类
社区版块
存档分类
最新评论

.Net 2.0 新功能:匿名方法(Anonymous Methods)

 
阅读更多

匿名方法(Anonymous Methods

<!--[if !supportLists]-->1. <!--[endif]-->2.0之前的c#版本中,声明委托的唯一方法是使用命名方法。

this.Load+=newSystem.EventHandler(this.Form1_Load);

protectedvoidForm1_Load(objectsender,EventArgse)

...{

MessageBox.Show(
"委托");

}




<!--[if !supportLists]-->2. <!--[endif]-->要将代码块传递为委托参数,创建匿名方法则是唯一的方法。

this.Load+=newdelegate(objectsender,EventArgse)...{MessageBox.Show("委托");}

<!--[if !supportLists]-->3. <!--[endif]-->使用匿名方法不需要再写一个方法,这个方法通常来说仅仅就是用来委托的,使用匿名方法将减少系统开销。

<!--[if !supportLists]-->4. <!--[endif]-->如果局部变量和参数的范围包含匿名方法声明,则局部变量和参数称为该匿名方法的外部变量或捕获变量。例如,下面代码段中的n即时一个外部变量:

intn=0;

Deld
=delegate()...{System.Console.Write(n);}

与局部变量不同,外部变量的声明周期一直持续到引用该匿名方法的委托符合垃圾回收的条件为止。对n的引用是在创建该委托时捕获的。

<!--[if !supportLists]-->5. <!--[endif]-->匿名方法不能访问外部范围的refout参数。

<!--[if !supportLists]-->6. <!--[endif]-->在匿名方法块中不能访问任何不安全代码。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics