码迷,mamicode.com
首页 > Windows程序 > 详细

C#之Lambda表达式

时间:2017-09-04 13:30:19      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:namespace   匿名函数   表达式   threading   str   open   符号   read   action   

Lambda表达式是为了更好的使用匿名函数,这里介绍一下lambda表达式的语法。

=> 是Lambda表达式必须的符号。=>左边代表函数的参数,右边为函数体。

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 MyPrograms
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             //如果lambda没有参数并且只有一句话的时候
14             Action a = () => Console.WriteLine("没有参数并且只有一句话的时候");
15             a();
16             //只有一个参数没有返回值
17             Action<int> b = num1 => Console.WriteLine(num1);
18             b(100);
19             //有多个参数,一个返回值
20             //Func尖括号里代表最后一个是返回值类型,其余为参数
21             Func<int, int, int> c = (num1, num2) => {
22                 Console.Write(num1);
23                 Console.Write("    ");
24                 Console.WriteLine(num2);
25                 return num1 + num2;
26             };
27             Console.WriteLine(c(5, 6));
28             Console.ReadKey();
29         }
30     }
31 }
View Code

运行结果如图:

技术分享

 

C#之Lambda表达式

标签:namespace   匿名函数   表达式   threading   str   open   符号   read   action   

原文地址:http://www.cnblogs.com/dawenhao/p/7472833.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!