Visual Studio 2010中的语言增强一览

开发 后端
Visual Studio 2010中有很多令人瞩目的新功能,而最基本的当然还是对VS.NET和C#的增强。本文介绍了一些Visual Studio 2010中的语言特性增强。

Visual Studio 2010的Beta版发布已经过了数月有余,无论是C#,VB.NET还是F#的表现都令人期待(有关Visual Studio 2010 Beta版中的新特性总览,可参考51CTO之前发布的这篇文章)。选择一种开发语言是程序员的个人选择,就像挑选一种冰激凌口味——有许多不错的选择,但你最喜欢的口味则是一个个人偏好的问题。

在Visual Studio 2010中,我们对两种最流行的语言:Visual Basic和C#做了一些加强,提供给程序员——无论他们偏好哪种语言——所有的工具,帮助他们去完成一个完美的软件。

Visual Studio 2010中的Visual Basic

Visual Basic团队致力于增强语言的效率,从而让开发者在更少的代码行中获得更多的功能。关于Visual Basic,最常见的用户需求是当将代码语句拆分为多行时,去除下划线(“_”)。Visual Basic 10 引入了隐式的行延续方式,使得在大多数情况下,不需要使用下划线。

  1. Function Filter(  
  2.                 ByVal customers As List(Of Customer),  
  3.                 ByVal orderCount As Integer 
  4.                )  
  5.  
  6.     Dimquery =  
  7.                 FromcIncustomers  
  8.       Where c.Orders.Count >  
  9.             orderCount  
  10.                 Selectc 

另一个提高效率的功能是自动实现(auto-implemented)属性。应用自动实现属性,原有的属性模板代码会被一行简单的声明代码替代。以前,属性声明一般是这样:

  1. Private _FavoriteFlavor As String = "Butter Pecan" 
  2.  
  3. Property FavoriteFlavor() As String 
  4.     Get 
  5.         Return _FavoriteFlavor  
  6.     End Get 
  7.     Set(ByVal value As String)  
  8.         _FavoriteFlavor = value  
  9.     End Set 
  10. End Property 
  11.  
  12. Private _FlavorList As New List(Of Flavor)  
  13.  
  14. Property FlavorList() As List(Of Flavor)  
  15.     Get 
  16.         Return _FlavorList  
  17.     End Get 
  18.     Set(ByVal value As String)  
  19.         _FlavorList = value  
  20.     End Set 
  21. End Property 

现在属性声明要简单的多:

  1. Property FavoriteFlavor As String = "Butter Pecan" 
  2. Property FlavorList As New List(Of Flavor) 

集合初始化以及数组标值同样很简单。现在集合可以在声明时初始化,并由编译器判断数组标值类型。

  1. Dim toppings = New List(Of String) From  
  2.     {  
  3.      "sprinkles",  
  4.      "chocolate chips",  
  5.      "strawberries" 
  6.     }  
  7. Dim cones = {"sugar cone""waffle cone"'the type String() is inferred 

Visual Basic 10.0对Lambda表达式有更好的支持。Lambda表达式可以包含不返回值的表达式,比如下文中的Sub关键字:

  1. Array.ForEach(toppings, Sub(n) Console.WriteLine(n)) 

有时你需要在Lambda表达式中做更复杂的工作,Visual Basic 10.0支持多行的Lambda表达式。和一般的表达式一样,编译器会推断出参数和返回值的类型:

  1. Dim doubleDown = Function(n As String)  
  2.                      If n.StartsWith("s"Then 
  3.                          Return "extra " & n  
  4.                      Else 
  5.                          Return n  
  6.                      End If 
  7.                  End Function 

在Visual Basic 10.0中,与Python或者Ruby这些动态语言的互操作也变得很简单。例如,下面的代码片段调用了Python库“math.py”中的一个方法:

  1. Dim mathLib As Object = python.UseFile("math.py")  
  2. Dim firstNumber = 44.2  
  3. Dim secondNumber = 9.5  
  4. mathLib.PowerOf(firstNumber, secondNumber) 

Visual Studio 2010中的C#

C#4.0主要的改进在于动态编程范式的共用性以及Office的可编程性改进。动态查询是C#4.0的新功能(有关C# 4.0动态类型的使用,可参考51CTO之前发布的这篇文章),这个功能使你可以以同样的方式使用并操作IronPython、IronRuby、JScript、HTML DOM或者标准.NET库中的对象。指定和可选的参数、COM客户端支持等语言方面的增强,则使得面向Office API的C#开发者能够享受Visual Basic开发者同样的美好体验。

在你的代码中添加dynamic这一新的关键词,能够使对象类型在运行时动态获得而不是在编译时制定。这样能够使动态语言以一种更自然的方式向C#暴露它们的对象。

  1. dynamic dynamicObject = GetDynamicObjectFromRuby();  
  2. dynamicObject.Foo(7);  
  3. dynamicObject.Property = "Property value";  
  4. dynamicObject[0] = "Indexed value";  
对于Visual Basic和C++的程序员来说,可选参数是很熟悉的,现在,C#程序员也可以使用这一特性。带有默认值的可选参数在方法签名中声明,如下所示:

  1. private void CreateNewStudent(string name, int currentCredits = 0, int year = 1)  
上述的方法可以用以下任意一种方式调用:  

  1. CreateNewStudent("Chloe");  
  2. CreateNewStudent("Zoe", 16);  
  3. CreateNewStudent("Joey", 40, 2);  
忽略currentCredits参数,但指定year参数,新的指定功能(高亮处)能够实现这一需求。下述调用同样可行:

  1. CreateNewStudent("Jill", year: 2);  
  2. CreateNewStudent(name: "Bill", currentCredits: 30, year: 2);  
  3. CreateNewStudent("Will", currentCredits: 4);  

指定参数同样是对已有方法书写自阐述调用的好方式,即使它们并没有使用可选参数。

【编辑推荐】

  1. Visual Studio 2010响应Ribbon控件消息
  2. 微软称C++将得到Visual Studio 2010更多支持
  3. Visual Studio 2010新特性:动态语言功能
  4. 微软对Visual Studio 2010的Bug熟视无睹?
  5. Visual Studio 2010 Beta1试用手记
责任编辑:yangsai 来源: 译言
相关推荐

2010-04-08 15:14:59

Visual StudASP.NET 4.

2009-05-19 09:21:50

Visual Stud云计算并行编程

2009-03-17 08:56:57

Visual StudVS2010C++

2009-09-02 16:21:17

Visual BasiC#语言

2009-12-02 09:43:38

Visual Stud

2012-02-20 09:06:20

JVM

2024-02-05 14:18:07

自然语言处理

2010-04-08 16:49:26

Visual StudMVC 2.0

2009-09-07 09:22:17

Visual Stud代码片段

2010-07-20 08:43:00

Visual Stud

2009-11-24 09:00:02

Visual Stud

2010-07-15 08:50:09

SharePointVisual Stud

2010-03-19 13:17:26

Parallel

2009-08-21 13:29:20

Visual Stud

2009-11-10 13:43:37

Visual Stud

2010-01-26 17:44:32

Visual C++开

2010-04-15 08:40:00

UML建模Visual Stud

2010-12-16 10:00:20

QtVisual Stud

2010-01-14 14:12:14

Visual Stud

2009-03-10 10:21:05

灾难恢复Restart Manvs
点赞
收藏

51CTO技术栈公众号