C# 匿名方法
我们已经提到过,委托是用于引用与其具有相同标签的方法。换句话说,您可以使用委托对象调用可由委托引用的方法。
匿名方法(Anonymous methods) 提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法。
在匿名方法中您不需要指定返回类型,它是从方法主体内的 return 语句推断的。
一、编写匿名方法的语法
匿名方法是通过使用 delegate 关键字创建委托实例来声明的。例如:
1 2 3 4 5 6 7 |
<span class="hljs-function"><span class="hljs-keyword">delegate</span> <span class="hljs-keyword">void</span> <span class="hljs-title">NumberChanger</span>(<span class="hljs-params"><span class="hljs-keyword">int</span> n</span>)</span>; ... NumberChanger nc = <span class="hljs-keyword">delegate</span>(<span class="hljs-keyword">int</span> x) { Console.WriteLine(<span class="hljs-string">"Anonymous Method: {0}"</span>, x); }; |
代码块 Console.WriteLine(“Anonymous Method: {0}”, x); 是匿名方法的主体。 委托可以通过匿名方法调用,也可以通过命名方法调用,即,通过向委托对象传递方法参数。 例如:
1 2 |
nc(<span class="hljs-number">10</span>); |
实例
下面的实例演示了匿名方法的概念:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<span class="hljs-keyword">using</span> System; <span class="hljs-function"><span class="hljs-keyword">delegate</span> <span class="hljs-keyword">void</span> <span class="hljs-title">NumberChanger</span>(<span class="hljs-params"><span class="hljs-keyword">int</span> n</span>)</span>; <span class="hljs-keyword">namespace</span> <span class="hljs-title">DelegateAppl</span> { <span class="hljs-keyword">class</span> <span class="hljs-title">TestDelegate</span> { <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> num = <span class="hljs-number">10</span>; <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">AddNum</span>(<span class="hljs-params"><span class="hljs-keyword">int</span> p</span>) </span>{ num += p; Console.WriteLine(<span class="hljs-string">"Named Method: {0}"</span>, num); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">MultNum</span>(<span class="hljs-params"><span class="hljs-keyword">int</span> q</span>) </span>{ num *= q; Console.WriteLine(<span class="hljs-string">"Named Method: {0}"</span>, num); } <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getNum</span>() </span>{ <span class="hljs-keyword">return</span> num; } <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Main</span>(<span class="hljs-params"><span class="hljs-keyword">string</span>[] args</span>) </span>{ <span class="hljs-comment">// 使用匿名方法创建委托实例</span> NumberChanger nc = <span class="hljs-keyword">delegate</span>(<span class="hljs-keyword">int</span> x) { Console.WriteLine(<span class="hljs-string">"Anonymous Method: {0}"</span>, x); }; <span class="hljs-comment">// 使用匿名方法调用委托</span> nc(<span class="hljs-number">10</span>); <span class="hljs-comment">// 使用命名方法实例化委托</span> nc = <span class="hljs-keyword">new</span> NumberChanger(AddNum); <span class="hljs-comment">// 使用命名方法调用委托</span> nc(<span class="hljs-number">5</span>); <span class="hljs-comment">// 使用另一个命名方法实例化委托</span> nc = <span class="hljs-keyword">new</span> NumberChanger(MultNum); <span class="hljs-comment">// 使用命名方法调用委托</span> nc(<span class="hljs-number">2</span>); Console.ReadKey(); } } } |
当上面的代码被编译和执行时,它会产生下列结果:
1 2 3 |
Anonymous Method: <span class="hljs-number">10</span> Named Method: <span class="hljs-number">15</span> Named Method: <span class="hljs-number">30</span> |