且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#委托基础8——lambda表达式

更新时间:2022-09-21 18:01:37

C#委托基础系列原于2011年2月份发表在我的新浪博客中,现在将其般至本博客。


  1. class Program  
  2. {  
  3.         double AddInt(int x, int y)  
  4.         {  
  5.             return x + y;  
  6.         }  
  7.  
  8.         string AddString(string s1, string s2)  
  9.         {  
  10.             return s1 + s2;  
  11.         }  
  12.  
  13.         static void Main(string[] args)  
  14.         {  
  15.             Program p = new Program();             
  16.  
  17.             // 以为前两个参数为int,他们运行的结果为double,最后一个参数与AddInt返回值一致  
  18.             Func<intintdouble> funcInt = p.AddInt;  
  19.             Console.WriteLine("funcInt的值为{0}", funcInt(100, 300));  
  20.  
  21.             Func<stringstringstring> funcString = p.AddString;  
  22.             Console.WriteLine("funcString的值为{0}", funcString("xy""xy"));  
  23.  
  24.  
  25.             // 匿名方法  
  26.             Func<floatfloatfloat> fucFloat = delegate(float x, float y)  
  27.             {  
  28.                 return x + y;  
  29.             };  
  30.             Console.WriteLine("funcFloat的值为{0}", fucFloat(190.7F, 99999.9F));  
  31.  
  32.             // Lambda表达式  
  33.             Func<stringstringstring> funString2 = (x, y) => (x + y);  
  34.             Console.WriteLine("funString2的值为{0}", funString2("xy""xy"));  
  35.             Console.ReadLine();  
  36.         }  

 本文转自IT徐胖子的专栏博客51CTO博客,原文链接http://blog.51cto.com/woshixy/1071021如需转载请自行联系原作者


woshixuye111