工作日志20081013(C#3.x相关)

1.LINQ是C#3中最闪亮的部分,扩展方法、lambda表达式、匿名类型等新的语法基本上都是围绕着为LINQ服务而诞生的。搞清楚这些语法对我们熟悉和掌握LINQ非常有帮助。其中,lambda表达式其实就是匿名的delegate。自己写了一个很小的demo,用来体现普通代理、匿名方法代理以及lambda表达式之间的关系。

 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Linq;
 4  using  System.Text;
 5  using  EagleFish.CommonLib;
 6 
 7  namespace  CommonConsoleProject
 8  {
 9       delegate  TResult GenericDelegateFunc < TArg, TResult > (TArg arg);
10 
11       class  Program
12      {
13           public   static   bool  SomeMethod( int  _arg)
14          {
15               return  _arg  >   0 ;
16          }
17 
18           static   void  Main( string [] args)
19          {
20              GenericDelegateFunc < int bool >  func  =   new  GenericDelegateFunc < int bool > (SomeMethod);
21              
22               // the use of Anonymous method, from C#2.0
23              GenericDelegateFunc < int bool >  funcUseAnonymousMethod  =   delegate ( int  arg)
24              {
25                   return  arg  >   0 ;
26              };
27 
28               // the use of Anonymous delegate(lambda expression),from C# 3.0
29              GenericDelegateFunc < int bool >  funcUseAnonymousDele  =  x  =>  {
30                  Console.WriteLine( " Now in the body of a lambda expression " );
31                   return  x  >   0 ;
32              };
33 
34              
35              Console.WriteLine(func( 3 ));
36              Console.WriteLine(funcUseAnonymousMethod( 5 ));
37              Console.WriteLine(funcUseAnonymousDele( - 6 ));
38 
39              Console.Read();
40          }
41      }
42  }

 由于C# 3的这些新特性都是对C#编译器的改进,并不涉及对IL的修改,所以有心的同学可以通过ILDasm看到这些代理的实现方式。

转载于:https://www.cnblogs.com/xingyukun/archive/2008/10/16/1310127.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值