使用lambda前pressions事件处理程序 [英] Using lambda expressions for event handlers
问题描述
我现在有这被宣布为页面如下:
I currently have a page which is declared as follows:
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//snip
MyButton.Click += (o, i) =>
{
//snip
}
}
}
我是最近才从1.1迁移到.NET 3.5,所以我已经习惯了写在Page_Load以外的事件处理程序。我的问题是;是否有任何性能缺点或缺陷,我应该注意使用此拉姆达方法时?我preFER它,因为它肯定更简洁,但我不想牺牲性能来使用它。谢谢你。
I've only recently moved to .NET 3.5 from 1.1, so I'm used to writing event handlers outside of the Page_Load. My question is; are there any performance drawbacks or pitfalls I should watch out for when using the lambda method for this? I prefer it, as it's certainly more concise, but I do not want to sacrifice performance to use it. Thanks.
推荐答案
有没有性能产生影响,因为编译器将你的lambda前pression转换成等价的委托。 LAMBDA前pressions只不过是一个语言功能编译器转换成您使用与工作完全一样的code以上。
There are no performance implications since the compiler will translate your lambda expression into an equivalent delegate. Lambda expressions are nothing more than a language feature that the compiler translates into the exact same code that you are used to working with.
编译器将在code你必须转换为这样的事情:
The compiler will convert the code you have to something like this:
public partial class MyPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//snip
MyButton.Click += new EventHandler(delegate (Object o, EventArgs a)
{
//snip
});
}
}
这篇关于使用lambda前pressions事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!