闽公网安备 35020302035485号
匿名方法的代价
匿名方法代价不低,因为它有 委托调用 方面的开销,什么意思呢?如果你的 lambda 里需要捕获封闭方法的局部变量或者参数,那么就会存在两种堆分配,一种是委托上的分配,另一种是闭包上的分配,如果你的 lambda 仅仅捕获一个封闭方法的实例状态,那么仅会有委托分配,如果你的 lambda 什么都不捕获或者仅捕获一个静态状态,那么就没有任何分配。int y = 1; MyMethod(x => x + y);上面代码的 lambda 中需要获取 y,所以就有了意想不到的堆分配,要想解决,可以将 y 定义为 const 或者 static 来避免这种不必要的分配开销,修改代码如下:
const int y = 1; MyMethod(static x => x + y);为了避免这种不必要和浪费内存的分配,可以在 lambda 上使用 static 关键词 及 变量上标注 const,值得注意的是,static 匿名函数不能访问封闭方法的局部变量和参数和 this 指针,但可以引用它的 静态方法 和 常量。
public class Demo
{
private string formattedText = "{0} It was developed by Microsoft's Anders Hejlsberg in the year 2000.";
void DisplayText(Func<string, string> func)
{
Console.WriteLine(func("C# is a popular programming language."));
}
public void Display()
{
DisplayText(text => string.Format(formattedText, text));
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
new Demo().Display();
Console.Read();
}
}
上面的例子中,formattedText变量会被 DisplayText 方法中的 func 所捕获,这也就意味着它会产生你意料之外的内存分配,把程序跑起来之后,会看到如下的输出。
public class Demo
{
private const string formattedText = "{0} It was developed by Microsoft's Anders Hejlsberg in the year 2000.";
void DisplayText(Func<string, string> func)
{
Console.WriteLine(func("C# is a popular programming language."));
}
public void Display()
{
DisplayText(static text => string.Format(formattedText, text));
Console.Read();
}
}
现在就没有任何你意料之外的分配了,我想这也是你想要的。