C# 3.0 Orcas 简介 (转)

文章来源:http://idior.cnblogs.com/archive/2005/09/14/237089.html

Implicitly typed local variables

var i  =   5 ;
var s 
=   " Hello " ;
var d 
=   1.0 ;
var numbers 
=   new   int []  {123} ;
var orders 
=   new  Dictionary < int ,Order > ();

局部变量的声明变得非常方便, 编译器会根据上下文自动推导出变量的类型.这个功能在3.0中被广泛使用.

局部变量的声明变得非常方便 , 编译器会根据上下文自动推导出变量的类型 . 这个功能在 3.0 中被广泛使用 .

下面的代码更加方便 :

var sum  =   0 ;
var intArray 
=   new  []  1,2,3,4} ;   // used to be  new int[](1,2,3,4)
foreach  ( var i  in  intArray )
{
    sum 
+= i;
}

 在匿名类型的支持下,你甚至可以这样玩.

var contacts  =   new []  {
new {
Name 
= "idior",
Website
= new[] "idior.cnblogs.com""www.alphatom.com" }
}
,

new {
Name 
= "dudu",
PhoneNumbers 
= new[] "www.aopdotnet.com" }
}

}
;

可见3.0下的代码是多么的简洁.

var contacts  =   new []  {
new {
Name 
= "idior",
Website
= new[] "idior.cnblogs.com""www.alphatom.com" }
}
,

new {
Name 
= "dudu",
PhoneNumbers 
= new[] "www.aopdotnet.com" }
}

}
;

可见3.0下的代码是多么的简洁.

var contacts  =   new []  {
new {
Name 
= "idior",
Website
= new[] "idior.cnblogs.com""www.alphatom.com" }
}
,

new {
Name 
= "dudu",
PhoneNumbers 
= new[] "www.aopdotnet.com" }
}

}
;

可见3.0下的代码是多么的简洁.

Extension methods

2.0的集合一文中, 我对新List加入的Foreach方法大加赞扬了一番, 同时也指出其不足, 并提供了一个扩展方法. 现在有了Extension Method的支持,要想对原有类扩展扩展功能简直太方便了. 

比如我想为所有的集合类加入Foreach的方法.

   24     class Static Algorithm

   25     {

   26         public static void ForEach(this IEnumerable collection, Action action)

   27         {

   28             foreach (T obj in collection)

   29             {

   30                 action(obj);

   31             }           

   32         }

   33 

   34         public static void ForSpecification(this IEnumerable collection, Action action, Predicate filter)

   35         {

   36             foreach (T obj in collection)

   37             {

   38                 if (filter(obj))

   39                     action(obj);

   40             }           

   41         }

   42     }

来看看如何使用:

var numbers  =   new  []  {“hello”, “world”, “Orcas”} ;

numbers.ForEach (
delegate ( string  str)    //  相当于Algorithm.ForEach(numbers,…)
             
                Console.WriteLine(str); 
            }
);   

Extension methods名副其实, 确实为功能扩展带来了极大的方便.

var numbers  =   new  []  {“hello”, “world”, “Orcas”} ;

numbers.ForEach (
delegate ( string  str)    //  相当于Algorithm.ForEach(numbers,…)
             
                Console.WriteLine(str); 
            }
);   

Extension methods名副其实, 确实为功能扩展带来了极大的方便.

: var numbers  =   new  []  {“hello”, “world”, “Orcas”} ;

numbers.ForEach (
delegate ( string  str)    //  相当于Algorithm.ForEach(numbers,…)
             
                Console.WriteLine(str); 
            }
);   

Extension methods名副其实, 确实为功能扩展带来了极大的方便.

名副其实, 确实为功能扩展带来了极大的方便.

Lambda Expressions

如果说你现在还没有习惯使用匿名方法, 没关系,一年后你将会在你的代码中到处用到它, 而Lambda Expressions则是对匿名方法的进一步增强, 两年后它将充斥我们的代码.

如果你看了我在CollectionClosureMethod in .Net 一文中利用匿名方法对Martin Fowler的CollectionClosureMethod in Ruby的模拟, 但是觉得很丑陋的话, 那么请你看看下面的代码: 

   21         managers = employees.select {|e| e.manager?} //ruby

   22 

   23         List managers = employees.FindAll(delegate(Employee e)                                             

   24                                                 {

   25                                                     return e.IsManager == true;

   26                                                 });      //C# 2.0

   27 

   28         var managers = employees.Select(e => e.IsManager==true);   //C# 3.0

   21         offices = employees.collect {|e| e.office}

   22 

   23         List offices = employees.ConvertAll(delegate(Employee e)                                                                                           

   24                                             {

   25                                                 return e.Office;

   26                                             });

   27 

   28         var offices = employees.Select(e => e.Office);


Object and Collection initializers

   21         managers = employees.select {|e| e.manager?} //ruby

   22 

   23         List managers = employees.FindAll(delegate(Employee e)                                             

   24                                                 {

   25                                                     return e.IsManager == true;

   26                                                 });      //C# 2.0

   27 

   28         var managers = employees.Select(e => e.IsManager==true);   //C# 3.0

   21         offices = employees.collect {|e| e.office}

   22 

   23         List offices = employees.ConvertAll(delegate(Employee e)                                                                                           

   24                                             {

   25                                                 return e.Office;

   26                                             });

   27 

   28         var offices = employees.Select(e => e.Office);


Object and Collection initializers

   21         managers = employees.select {|e| e.manager?} //ruby

   22 

   23         List managers = employees.FindAll(delegate(Employee e)                                             

   24                                                 {

   25                                                     return e.IsManager == true;

   26                                                 });      //C# 2.0

   27 

   28         var managers = employees.Select(e => e.IsManager==true);   //C# 3.0

   21         offices = employees.collect {|e| e.office}

   22 

   23         List offices = employees.ConvertAll(delegate(Employee e)                                                                                           

   24                                             {

   25                                                 return e.Office;

   26                                             });

   27 

   28         var offices = employees.Select(e => e.Office);


Object and Collection initializers

对象的初始化的代码将变得异常简洁.

var idior  =   new  Person {Name = "ning", Surname = "xu"} ;

var persons 
=   new  List < Person >  
    
new Person{Name = "ning", Surname = "xu"}
    
new Person{Name = "Foo", Surname = "Bar"}
}


Anonymous types

var idior  =   new  Person {Name = "ning", Surname = "xu"} ;

var persons 
=   new  List < Person >  
    
new Person{Name = "ning", Surname = "xu"}
    
new Person{Name = "Foo", Surname = "Bar"}
}


Anonymous types

var idior  =   new  Person {Name = "ning", Surname = "xu"} ;

var persons 
=   new  List < Person >  
    
new Person{Name = "ning", Surname = "xu"}
    
new Person{Name = "Foo", Surname = "Bar"}
}


Anonymous types

var idior  =   new   {Name = "idior", WebSite = http://idior.cnblogs.com/};
Console.WriteLine(idior.WebSite); 


Query Expressions


Query Expressions

将关系型查询语句引入面向对象的世界, 大大增强了语法安全性, 很久以前我也对此有过介绍..

from c  in  customers
where c.City 
==   " London "
select c

相应的Lambda表达式描述


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部