匿名方法的使用

匿名方法相信很多人都听过,它是C#2.0的一个新特性,顾名思义,匿名方法就是没有名称的方法。那么在C#中的匿名方法有哪些好处,在C#中如何使用呢?
匿名方法最明显的好处就是可以降低另写一个方法的工作量,另外一个好处就是可以访问调用者的变量,降低传参数的复杂度,下面就通过一些使用例子来具体看看。
1、在事件中使用匿名方法
下面是一个定时器的小例子,我们常规的写法如下:
匿名方法的使用匿名方法的使用常规写法
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->匿名方法的使用classEventTest
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
publicvoidTest()
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用System.Timers.TimertimersTimer
=newSystem.Timers.Timer();
匿名方法的使用
匿名方法的使用timersTimer.Enabled
=true;
匿名方法的使用timersTimer.Interval
=5000;
匿名方法的使用timersTimer.Elapsed
+=newSystem.Timers.ElapsedEventHandler(timersTimer_Elapsed);
匿名方法的使用Console.ReadLine();
匿名方法的使用}

匿名方法的使用
匿名方法的使用
voidtimersTimer_Elapsed(objectsender,System.Timers.ElapsedEventArgse)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用Console.WriteLine(System.DateTime.Now);
匿名方法的使用}

匿名方法的使用}

匿名方法的使用
对于事件的处理我们需要单独写一个方法timersTimer_Elapsed,那么如果使用匿名方法,就可以省掉这个方法的定义,如下所示:
匿名方法的使用classEventTest
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
publicvoidTest()
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用System.Timers.TimertimersTimer
=newSystem.Timers.Timer();
匿名方法的使用
匿名方法的使用timersTimer.Enabled
=true;
匿名方法的使用timersTimer.Interval
=5000;
匿名方法的使用timersTimer.Elapsed
+=
匿名方法的使用
delegate(objectsender,System.Timers.ElapsedEventArgse)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用Console.WriteLine(System.DateTime.Now);
匿名方法的使用}
;
匿名方法的使用Console.ReadLine();
匿名方法的使用}

匿名方法的使用}

匿名方法的使用
也就是把方法的实现直接写在内部。

2、在我们自己的代理中使用匿名方法
比如如下一个字符串转化的例子,按照传统的方法来写的话,对于每一种转化规则都需要单独定义一个方法:
匿名方法的使用匿名方法的使用常规写法
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->匿名方法的使用classDelegateTest
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
delegatestringConvert(stringtext);
匿名方法的使用
匿名方法的使用
publicvoidTest()
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
stringstrText="123451234512345";
匿名方法的使用
匿名方法的使用Test2(ConvertA,strText);
匿名方法的使用Test2(ConvertB,strText);
匿名方法的使用Test2(ConvertC,strText);
匿名方法的使用
匿名方法的使用Console.ReadLine();
匿名方法的使用}

匿名方法的使用
匿名方法的使用
privatevoidTest2(Convertconvert,stringstrText)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用Console.WriteLine(convert(strText));
匿名方法的使用}

匿名方法的使用
匿名方法的使用
publicstringConvertA(stringstrText)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText.Replace("1","A");
匿名方法的使用}

匿名方法的使用
publicstringConvertB(stringstrText)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText.Replace("1","B");
匿名方法的使用}

匿名方法的使用
publicstringConvertC(stringstrText)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText.Replace("1","C");
匿名方法的使用}

匿名方法的使用}
我们的例子中有三种规则,那么就要定义三个方法,如果使用匿名方法的话,代码就会很简单:
匿名方法的使用classDelegateTest
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
delegatestringConvert(stringtext);
匿名方法的使用
匿名方法的使用
publicvoidTest()
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
stringstrText="123451234512345";
匿名方法的使用
匿名方法的使用Test2(
delegate(stringstrText2)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText2.Replace("1","A");
匿名方法的使用}
,strText);
匿名方法的使用Test2(
delegate(stringstrText2)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText2.Replace("1","B");
匿名方法的使用}
,strText);
匿名方法的使用Test2(
delegate(stringstrText2)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText2.Replace("1","C");
匿名方法的使用}
,strText);
匿名方法的使用
匿名方法的使用Console.ReadLine();
匿名方法的使用}

匿名方法的使用
匿名方法的使用
privatevoidTest2(Convertconvert,stringstrText)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用Console.WriteLine(convert(strText));
匿名方法的使用}

匿名方法的使用}

3、优化例子2,不再传递参数
在例子2中我们是把参数传到你们方法内部的,其实在匿名方法内部可以直接取得当前调用者的变量,节省了传递参数的代码量:
匿名方法的使用classAnonTest
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
delegatestringConvert();
匿名方法的使用
匿名方法的使用
publicvoidTest()
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
stringstrText="123451234512345";
匿名方法的使用
匿名方法的使用Test2(
delegate()
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText.Replace("1","A");
匿名方法的使用}
);
匿名方法的使用Test2(
delegate()
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText.Replace("1","B");
匿名方法的使用}
);
匿名方法的使用Test2(
delegate()
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
returnstrText.Replace("1","C");
匿名方法的使用}
);
匿名方法的使用
匿名方法的使用Console.ReadLine();
匿名方法的使用}

匿名方法的使用
匿名方法的使用
privatevoidTest2(Convertconvert)
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用Console.WriteLine(convert());
匿名方法的使用}

匿名方法的使用}
这样一来,代码是不是看起来很整洁:)

当然代码虽然看起来很少,实际上编译器在编译时还是会生成其他方法的。也就是说匿名方法可以减少代码量,节省开发时间,但是对于性能方法没有什么提升的。
我们通过IL DASM工具可以查看一下AnonTest类编译后的代码,会发现增加一个新类,在这个类里面生成了三个方法和一个同名的strText变量:
匿名方法的使用

然后在Test方法中会调用这些新生成的方法,Test如下所示:
匿名方法的使用.methodpublichidebysiginstancevoidTest()cilmanaged
匿名方法的使用匿名方法的使用
匿名方法的使用{
匿名方法的使用
//代码大小83(0x53)
匿名方法的使用
.maxstack4
匿名方法的使用.localsinit([
0]classAnonMethod.AnonTest/'<>c__DisplayClass3''<>8__locals4')
匿名方法的使用IL_0000:newobjinstance
voidAnonMethod.AnonTest/'<>c__DisplayClass3'::.ctor()
匿名方法的使用IL_0005:stloc.
0
匿名方法的使用IL_0006:nop
匿名方法的使用IL_0007:ldloc.
0
匿名方法的使用IL_0008:ldstr
"123451234512345"
匿名方法的使用IL_000d:stfld
stringAnonMethod.AnonTest/'<>c__DisplayClass3'::strText
匿名方法的使用IL_0012:ldarg.
0
匿名方法的使用IL_0013:ldloc.
0
匿名方法的使用IL_0014:ldftninstance
stringAnonMethod.AnonTest/'<>c__DisplayClass3'::'<Test>b__0'()
匿名方法的使用IL_001a:newobjinstance
voidAnonMethod.AnonTest/Convert::.ctor(object,
匿名方法的使用native
int)
匿名方法的使用IL_001f:callinstance
voidAnonMethod.AnonTest::Test2(classAnonMethod.AnonTest/Convert)
匿名方法的使用IL_0024:nop
匿名方法的使用IL_0025:ldarg.
0
匿名方法的使用IL_0026:ldloc.
0
匿名方法的使用IL_0027:ldftninstance
stringAnonMethod.AnonTest/'<>c__DisplayClass3'::'<Test>b__1'()
匿名方法的使用IL_002d:newobjinstance
voidAnonMethod.AnonTest/Convert::.ctor(object,
匿名方法的使用native
int)
匿名方法的使用IL_0032:callinstance
voidAnonMethod.AnonTest::Test2(classAnonMethod.AnonTest/Convert)
匿名方法的使用IL_0037:nop
匿名方法的使用IL_0038:ldarg.
0
匿名方法的使用IL_0039:ldloc.
0
匿名方法的使用IL_003a:ldftninstance
stringAnonMethod.AnonTest/'<>c__DisplayClass3'::'<Test>b__2'()
匿名方法的使用IL_0040:newobjinstance
voidAnonMethod.AnonTest/Convert::.ctor(object,
匿名方法的使用native
int)
匿名方法的使用IL_0045:callinstance
voidAnonMethod.AnonTest::Test2(classAnonMethod.AnonTest/Convert)
匿名方法的使用IL_004a:nop
匿名方法的使用IL_004b:call
string[mscorlib]System.Console::ReadLine()
匿名方法的使用IL_0050:pop
匿名方法的使用IL_0051:nop
匿名方法的使用IL_0052:ret
匿名方法的使用}
//endofmethodAnonTest::Test