.NetFramework 3.5 之Lambda表达式

   Lambda 表达式(Lambda Expressions )是LINQ 实现的另一特性。Lambda 表达式的作用就是使用使用函数式语法,将方法实现关联到委托实例。在使 用查询表达式(Query Expressions )时,查询表达式也将被语法映射为扩展方法(Extension Methods )和Lambda 表 达式配合使用的形式。

    先来说说Lambda 表达式的“前身”——匿名方法(Anonymous Methods )。匿名方法是.NET Framework 2.0 中的新特性,意 义就在于使用更简单的语法将方法实现关联到委托(delegate )实例。例如:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

// 匿名 方法演示

namespace MyConsoleApplication

{

    class Program

    {

        delegate void MyDelegate (string x, int y);

 

        // 自动属性

        static string Name { get ; set ; }

 

        static int Age { get ; set ; }

 

        static void Main(string [] args)

        {

            // 匿名方法实现MyDelegate 委托

             MyDelegate anonymousMethod = delegate (string x, int y) { Name = x; Age = y; };

            MyOtherMethod(anonymousMethod);

            MyOtherMethod(delegate (string x, int y) { Name = x + " second" ; Age = y; });

            Console .Read();

        }

 

         static void MyOtherMethod(MyDelegate myDelegate)

        {

            myDelegate("set in delegate" , 20);

            Console .WriteLine(string .Format("Name:{0} | Age:{1}" , Name, Age.ToString()));

        }

    }

}

 

结果显示:

Name:set in delegate | Age:20

Name:set in delegate second | Age:20

如果使用Lambda 表达式实现相同的功能,语法相比匿名方法要简单一些,如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

// Lambda 表达式演示

namespace MyConsoleApplication

{

    class Program

    {

        delegate void MyDelegate (string x, int y);

 

        // 自动属性

        static string Name { get ; set ; }

 

        static int Age { get ; set ; }

 

        static void Main(string [] args)

        {

            // Lambda 表达式实现MyDelegate 委托

            MyDelegate anonymousMethod = (string x, int y) => { Name = x; Age = y; };

            MyOtherMethod(anonymousMethod);

            MyOtherMethod((x, y) => { Name = x + " second" ; Age = y; });

            Console .Read();

        }

 

        static void MyOtherMethod(MyDelegate myDelegate)

        {

            myDelegate("set in delegate" , 20);

            Console .WriteLine(string .Format("Name:{0} | Age:{1}" , Name, Age.ToString()));

        }

    }

}

 

结果显示:

Name:set in delegate | Age:20

Name:set in delegate second | Age:20

查看IL 代 码,使用Lambda 表达式与使用匿名方法的IL 代 码一致,可以说,Lambda 表达式最终会编译成匿名方法的IL 代 码,只是语法上的区别罢了。

————————— 华丽的分割线 ——————————

    简单总结:

1.       Lambda 表达式的基本形式是:( 参数列表)=> 表达式或语句块。

2.       Lambda 表达式的特点就是:参数列表可以不指定参数类型,由编译器进行推断。但需要注意,在多个参数的情况 下,如果不显示指定参数类型,所有的参数都不应指定参数类型;如果显示指定参数类型,所有的参数都应显示指定参数类型。

3.       如果委托有返回类型,可以再语句块中进行返回。如果委托无参数,Lambda 表达式的语法为:()=> 表达式或语 句块。示例如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

// Lambda 表达式演示

namespace MyConsoleApplication

{

    class Program

    {

        delegate int MyDelegate1 (int y);

 

        delegate void MyDelegate2 ();

 

        static void Main(string [] args)

        {

            // 有返回值

            MyDelegate1 my1 = y => { return y + 10; };

            // 无参数

            int x = 10;

            MyDelegate2 my2 = () => x = 10;

        }

    }

}

————————— 华丽的分割线 ——————————

Lambda 表达式树(Lambda Expressions Tree ) 简单介绍:

Lambda 表达式树就是将Lambda 表达式分解成树状 结构(还没有想到具体的用途... )。使用Lambda 表 达式树,需要引入System.Linq.Expressions 命名空间;如果分解Lambda 表达式,表达式主体(=> 右边部分) 不能是语句块或等式。示例如下:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Linq.Expressions;

 

// Lambda 表达式树演示

// 获得 表达式实例后就可以进行分解了

namespace MyConsoleApplication

{

    class Program

    {

        delegate int MyDelegate1 (int y);

 

        delegate bool MyDelegate2 ();

 

        static void Main(string [] args)

        {

            // 获得返回int 型的Lambda 表达式实例

            Expression <MyDelegate1 > my1 = y => y + 10;

            // 获得返回bool 型的Lambda 表达式实例

            int x = 10;

            Expression <MyDelegate2 > my2 = () => x > 10;

            // 利用系统委托Func 获得返回bool 型的Lambda 表达式实例

            // Func 泛型委托很重要

            Expression <Func <bool >> sys1 = () => 1 > 2;

        }

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值