什么是.net中的匿名方法以及为什么使用匿名方法 [英] what is anonymous methods in .net and why used anonymous method
本文介绍了什么是.net中的匿名方法以及为什么使用匿名方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任何人都可以用敏锐的例子简单地解释一下吗? .net中的匿名方法是什么,为什么使用匿名方法
Can any one explain this in brief with sharp example
what is anonymous methods in .net and why used anonymous method
推荐答案
创建匿名方法本质上是一种将代码块作为委托参数传递的方法.例如:
复制//为点击事件创建处理程序
button1.Click + =委托(System.Object o,System.EventArgs e)
{System.Windows.Forms.MessageBox.Show("Click!"); };
或
复制//创建委托实例
委托void Del(int x);
//使用匿名方法实例化委托
Del d =委托人(int k){/* ... */};
通过使用匿名方法,无需创建单独的方法,从而减少了实例化委托时的编码开销.
Creating anonymous methods is essentially a way to pass a code block as a delegate parameter. For example:
Copy// Create a handler for a click event
button1.Click += delegate(System.Object o, System.EventArgs e)
{ System.Windows.Forms.MessageBox.Show("Click!"); };
or
Copy// Create a delegate instance
delegate void Del(int x);
// Instantiate the delegate using an anonymous method
Del d = delegate(int k) { /* ... */ };
By using anonymous methods, you reduce the coding overhead in instantiating delegates by eliminating the need to create a separate method.
这篇关于什么是.net中的匿名方法以及为什么使用匿名方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文