C# 2.0中引入了匿名方法,但在C#3.0及更高版本中,Lambda表达式代替了匿名方法。
不过,本主题中有关匿名方法的信息同样也适用于 Lambda 表达式。 有一种情况下,匿名方法提供了 Lambda 表达式中所没有的功能。 您可使用匿名方法来忽略参数列表。 这意味着匿名方法可转换为具有各种签名的委托。 这对于 Lambda 表达式来说是不可能的。
/*************************************************案例1*******************************************/
<span style="font-size:10px;"> this.Invoke(new EventHandler(</span>
<span style="font-size:10px;"><span style="white-space:pre"> </span>delegate{
// XXXXXXXX;
})<span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;">);</span></span>
<span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;"><span style="font-size:10px;">
</span></span>
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;font-size:10px;">/***********************************************************案例2********************************************************/</span>
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;"></span><pre style="padding: 5px; margin-top: 0px; margin-bottom: 0px; overflow: auto; word-wrap: normal; line-height: 17.549999237060547px; font-family: Consolas, Courier, monospace !important;"><span style="font-size:10px;">System.Threading.Thread t1 = <span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; color: blue;">new</span> System.Threading.Thread(new ThreadStart(<span style="font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important; color: blue;">delegate</span>()
{
// XXXXXXXX;
}));
t1.Start();</span>
Lambda表达式的话
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;"></span><pre style="margin-top: 0px; margin-bottom: 0px; color: rgb(42, 42, 42); padding: 5px; overflow: auto; word-wrap: normal; line-height: 17.549999237060547px; font-family: Consolas, Courier, monospace !important;"><span style="font-size:10px;">System.Threading.Thread t1 = <span style="color: blue; font-family: 'Microsoft YaHei UI', 'Microsoft YaHei', SimSun, 'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif !important;">new</span> System.Threading.Thread(new ThreadStart(()=>
{
// XXXXXXXX;
}));
t1.Start();</span>
/*************************************************************案例3*********************************************************/
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;">using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
public delegate bool MYDEL();
public class Program
{
public MYDEL mydel = null;
static void Main(string[] args)
{
if (new Program() { mydel = new MYDEL(delegate() { Console.Write("HELLO! "); return false; }) }.mydel())</span>
<span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;"><span style="white-space:pre"> </span> //</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span><span style="font-family: 'Courier New'; white-space: pre-wrap;"> (</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">new</span><span style="font-family: 'Courier New'; white-space: pre-wrap;"> Func<</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">bool</span><span style="font-family: 'Courier New'; white-space: pre-wrap;">>(() => { Console.Write(</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(128, 0, 0); line-height: 1.5 !important;">Hello </span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="font-family: 'Courier New'; white-space: pre-wrap;">); </span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span><span style="font-family: 'Courier New'; white-space: pre-wrap;"> </span><span style="font-family: 'Courier New'; white-space: pre-wrap; color: rgb(0, 0, 255); line-height: 1.5 !important;">false</span><span style="font-family: 'Courier New'; white-space: pre-wrap;">; }).Invoke())</span><span style="font-family:Microsoft YaHei UI, Microsoft YaHei, SimSun, Segoe UI, Lucida Grande, Verdana, Arial, Helvetica, sans-serif;">
{
Console.Write("HELLO!");
}
else
{
Console.Write("WORLD!");
Console.Read();
}
}
}
}
</span>
匿名委托关键字还是delegate