C#匿名方法

C# 匿名方法

我们已经提到过,委托是用于引用与其具有相同标签的方法。换句话说,您可以使用委托对象调用可由委托引用的方法。

匿名方法(Anonymous methods) 提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法。

在匿名方法中您不需要指定返回类型,它是从方法主体内的 return 语句推断的。

编写匿名方法的语法

匿名方法是通过使用 delegate 关键字创建委托实例来声明的。例如:

[csharp]  view plain  copy
  1. delegate void NumberChanger(int n);  
  2. ...  
  3. NumberChanger nc = delegate(int x)  
  4. {  
  5.     Console.WriteLine("Anonymous Method: {0}", x);  
  6. };  

代码块 Console.WriteLine("Anonymous Method: {0}", x); 是匿名方法的主体。

委托可以通过匿名方法调用,也可以通过命名方法调用,即,通过向委托对象传递方法参数。

例如:

[csharp]  view plain  copy
  1. nc(10);  

匿名方法与委托的实例

下面的实例演示了匿名方法的概念:

[csharp]  view plain  copy
  1. using System;  
  2.   
  3. delegate void NumberChanger(int n);  
  4. namespace DelegateAppl  
  5. {  
  6.     class TestDelegate  
  7.     {  
  8.         static int num = 10;  
  9.         public static void AddNum(int p)  
  10.         {  
  11.             num += p;  
  12.             Console.WriteLine("Named Method: {0}", num);  
  13.         }  
  14.   
  15.         public static void MultNum(int q)  
  16.         {  
  17.             num *= q;  
  18.             Console.WriteLine("Named Method: {0}", num);  
  19.         }  
  20.         public static int getNum()  
  21.         {  
  22.             return num;  
  23.         }  
  24.   
  25.         static void Main(string[] args)  
  26.         {  
  27.             // 使用匿名方法创建委托实例  
  28.             NumberChanger nc = delegate(int x)  
  29.             {  
  30.                Console.WriteLine("Anonymous Method: {0}", x);  
  31.             };  
  32.               
  33.             // 使用匿名方法调用委托  
  34.             nc(10);  
  35.   
  36.             // 使用命名方法实例化委托  
  37.             nc =  new NumberChanger(AddNum);  
  38.               
  39.             // 使用命名方法调用委托  
  40.             nc(5);  
  41.   
  42.             // 使用另一个命名方法实例化委托  
  43.             nc =  new NumberChanger(MultNum);  
  44.               
  45.             // 使用命名方法调用委托  
  46.             nc(2);  
  47.             Console.ReadKey();  
  48.         }  
  49.     }  
  50. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. Anonymous Method: 10  
  2. Named Method: 15  
  3. Named Method: 30  


C# 匿名方法

我们已经提到过,委托是用于引用与其具有相同标签的方法。换句话说,您可以使用委托对象调用可由委托引用的方法。

匿名方法(Anonymous methods) 提供了一种传递代码块作为委托参数的技术。匿名方法是没有名称只有主体的方法。

在匿名方法中您不需要指定返回类型,它是从方法主体内的 return 语句推断的。

编写匿名方法的语法

匿名方法是通过使用 delegate 关键字创建委托实例来声明的。例如:

[csharp]  view plain  copy
  1. delegate void NumberChanger(int n);  
  2. ...  
  3. NumberChanger nc = delegate(int x)  
  4. {  
  5.     Console.WriteLine("Anonymous Method: {0}", x);  
  6. };  

代码块 Console.WriteLine("Anonymous Method: {0}", x); 是匿名方法的主体。

委托可以通过匿名方法调用,也可以通过命名方法调用,即,通过向委托对象传递方法参数。

例如:

[csharp]  view plain  copy
  1. nc(10);  

匿名方法与委托的实例

下面的实例演示了匿名方法的概念:

[csharp]  view plain  copy
  1. using System;  
  2.   
  3. delegate void NumberChanger(int n);  
  4. namespace DelegateAppl  
  5. {  
  6.     class TestDelegate  
  7.     {  
  8.         static int num = 10;  
  9.         public static void AddNum(int p)  
  10.         {  
  11.             num += p;  
  12.             Console.WriteLine("Named Method: {0}", num);  
  13.         }  
  14.   
  15.         public static void MultNum(int q)  
  16.         {  
  17.             num *= q;  
  18.             Console.WriteLine("Named Method: {0}", num);  
  19.         }  
  20.         public static int getNum()  
  21.         {  
  22.             return num;  
  23.         }  
  24.   
  25.         static void Main(string[] args)  
  26.         {  
  27.             // 使用匿名方法创建委托实例  
  28.             NumberChanger nc = delegate(int x)  
  29.             {  
  30.                Console.WriteLine("Anonymous Method: {0}", x);  
  31.             };  
  32.               
  33.             // 使用匿名方法调用委托  
  34.             nc(10);  
  35.   
  36.             // 使用命名方法实例化委托  
  37.             nc =  new NumberChanger(AddNum);  
  38.               
  39.             // 使用命名方法调用委托  
  40.             nc(5);  
  41.   
  42.             // 使用另一个命名方法实例化委托  
  43.             nc =  new NumberChanger(MultNum);  
  44.               
  45.             // 使用命名方法调用委托  
  46.             nc(2);  
  47.             Console.ReadKey();  
  48.         }  
  49.     }  
  50. }  

当上面的代码被编译和执行时,它会产生下列结果:

[csharp]  view plain  copy
  1. Anonymous Method: 10  
  2. Named Method: 15  
  3. Named Method: 30  


猜你喜欢

转载自blog.csdn.net/weixin_39029925/article/details/78212586