C# - Lambda表达式

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 委托_Lambda表达式
 8 {
 9     class Program
10     {
11         // 声明委托
12         delegate double Calculate(double x, double y);
13         static void Main(string[] args)
14         {
15             /***********************************************************************
16              * Lambda表达式:
17              *      1. 参数个数和类型必须和委托保持一致;
18              *      2. 可以省略形参类型;
19              *      3. 可以省略大括号
20             ***********************************************************************/
21             Calculate cal = (double a, double b) => a + b;
22             Console.WriteLine( "Lambda表达式, 加法运算: {0}", cal(100,200) );
23 
24             // 省略形参类型
25             cal = (a, b) => a * b;
26             Console.WriteLine("Lambda表达式, 乘法运算: {0}", cal(100,200) );
27 
28             Console.ReadLine();
29 
30         }
31     }
32 }

posted @ 2016-04-02 11:12  C/C++/Python/Java  阅读(376)  评论(0编辑  收藏  举报