首页 > Windows开发 > 详细

C#之 Lambda表达式

时间:2016-06-13 15:21:58      阅读:208      评论:0      收藏:0      [点我收藏+]

Lambda表达式 简化了匿名委托的使用,让你让代码更加简洁,优雅。据说它是微软自c#1.0后新增的最重要的功能之一。

首先来看一下其发展

  技术分享

  根据上面的发展历程,可以感到Lambda表达式愈加简化。

详细介绍:

  技术分享

技术分享

 

技术分享

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lambda
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            Func<int, int> f1 = x => 2 * x + 1;
            Func<int, int, bool> f2 = (x, y) => x > y;
            Func<string, int, string> f3 = (x, y) => x.Substring(y);
            Func<int, int> f4 = (x) => sum += x;
            Action a1 = () => { System.Console.WriteLine("HelloWorld"); };
            Action <int> a2 = (x) => { System.Console.WriteLine(x); };
            Action <bool> a3 = (x) => { System.Console.WriteLine(x); };
            Action<string> a4 = (x) => { System.Console.WriteLine(x); };

            for (int i = 1; i <= 10; i++ )
            {
                f4(i);
            }
            a2(sum);
            a1();
            a2(f1(1));
            a3(f2(3, 5));
            a4(f3("Zhengpengfei", 5));
            System.Console.Read();
        }
    }
}

技术分享

C#之 Lambda表达式

原文:http://www.cnblogs.com/zpfbuaa/p/5580593.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!