<div class="blogpost-body" id="cnblogs_post_body">
<p>匿名方法是C# 2.0中简化委托模型的一种语法糖。Lambda表达式是C# 3.0新增的语法特性,其在匿名方法的基础上更进一步,但其本质都是相同的,我们通过一段代码来分析对比这个语言特性。它们分别是在C#2.0与C# 3.0中的主要写法。</p>
<div class="cnblogs_code">
<pre class="blockcode"><span style="color:#008080;"> 1</span> <span style="color:#0000ff;">delegate</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> DelWithoutParam();
</span><span style="color:#008080;"> 2</span> <span style="color:#0000ff;">delegate</span> <span style="color:#0000ff;">void</span> DelWithParams(<span style="color:#0000ff;">int</span> intparam, <span style="color:#0000ff;">string</span><span style="color:#000000;"> strparam);
</span><span style="color:#008080;"> 3</span> <span style="color:#0000ff;">delegate</span> <span style="color:#0000ff;">string</span> DelParamRetVal(<span style="color:#0000ff;">int</span><span style="color:#000000;"> param);
</span><span style="color:#008080;"> 4</span>
<span style="color:#008080;"> 5</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> Program
</span><span style="color:#008080;"> 6</span> <span style="color:#000000;">{
</span><span style="color:#008080;"> 7</span> <span style="color:#0000ff;">public</span><span style="color:#000000;"> DelWithoutParam delTestNoParam;
</span><span style="color:#008080;"> 8</span> <span style="color:#0000ff;">public</span><span style="color:#000000;"> DelWithParams delTestWithParam;
</span><span style="color:#008080;"> 9</span> <span style="color:#0000ff;">public</span><span style="color:#000000;"> DelParamRetVal delTestParamRetVal;
</span><span style="color:#008080;">10</span>
<span style="color:#008080;">11</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">event</span><span style="color:#000000;"> DelWithoutParam evtTestNoParam;
</span><span style="color:#008080;">12</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">event</span><span style="color:#000000;"> DelWithParams evtTestWithParam;
</span><span style="color:#008080;">13</span>
<span style="color:#008080;">14</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span><span style="color:#000000;">[] args)
</span><span style="color:#008080;">15</span> <span style="color:#000000;"> {
</span><span style="color:#008080;">16</span> Program p = <span style="color:#0000ff;">new</span><span style="color:#000000;"> Program();
</span><span style="color:#008080;">17</span> <span style="color:#008000;">//</span><span style="color:#008000;">订阅处理函数</span>
<span style="color:#008080;">18</span> <span style="color:#000000;"> p.SubEvent();
</span><span style="color:#008080;">19</span> <span style="color:#000000;"> p.ShowVarible();
</span><span style="color:#008080;">20</span>
<span style="color:#008080;">21</span> <span style="color:#000000;"> Console.Read();
</span><span style="color:#008080;">22</span> <span style="color:#008000;">//</span><span style="color:#008000;">依次触发委托与事件</span>
<span style="color:#008080;">23</span> <span style="color:#000000;"> p.delTestNoParam();
</span><span style="color:#008080;">24</span> p.delTestWithParam(<span style="color:#800080;">1</span>, <span style="color:#800000;">"</span><span style="color:#800000;">ParamInDelobj</span><span style="color:#800000;">"</span><span style="color:#000000;">);
</span><span style="color:#008080;">25</span> <span style="color:#008000;">//</span><span style="color:#008000;">只会返回第二个调用函数的返回值</span>
<span style="color:#008080;">26</span> Console.WriteLine(p.delTestParamRetVal(<span style="color:#800080;">10086</span><span style="color:#000000;">));
</span><span style="color:#008080;">27</span>
<span style="color:#008080;">28</span> <span style="color:#000000;"> p.evtTestNoParam();
</span><span style="color:#008080;">29</span> p.evtTestWithParam(<span style="color:#800080;">1</span>, <span style="color:#800000;">"</span><span style="color:#800000;">ParamInEvt</span><span style="color:#800000;">"</span><span style="color:#000000;">);
</span><span style="color:#008080;">30</span>
<span style="color:#008080;">31</span> <span style="color:#008000;">//</span><span style="color:#008000;">Console.ReadLine();</span>
<span style="color:#008080;">32</span> Thread.Sleep(<span style="color:#800080;">100000</span><span style="color:#000000;">);
</span><span style="color:#008080;">33</span> <span style="color:#000000;"> }
</span><span style="color:#008080;">34</span>
<span style="color:#008080;">35</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> SubEvent()
</span><span style="color:#008080;">36</span> <span style="color:#000000;"> {
</span><span style="color:#008080;">37</span> <span style="color:#008000;">//</span><span style="color:#008000;">不具有参数的委托声明,在delegate后面加上"()"</span>
<span style="color:#008080;">38</span> |
|