匿名方法(Anonymous Methods)
1. 在2.0之前的c#版本中,声明委托的唯一方法是使用命名方法。












2. 要将代码块传递为委托参数,创建匿名方法则是唯一的方法。

3. 使用匿名方法不需要再写一个方法,这个方法通常来说仅仅就是用来委托的,使用匿名方法将减少系统开销。
4. 如果局部变量和参数的范围包含匿名方法声明,则局部变量和参数称为该匿名方法的外部变量或捕获变量。例如,下面代码段中的n即时一个外部变量:




与局部变量不同,外部变量的声明周期一直持续到引用该匿名方法的委托符合垃圾回收的条件为止。对n的引用是在创建该委托时捕获的。
5. 匿名方法不能访问外部范围的ref或out参数。
6. 在匿名方法块中不能访问任何不安全代码。