日期:2014-05-20  浏览次数:20774 次

微软VS2010中VB.NET的新特性
VS2010中vb.net的新特性
1.取消了连接符
  大家都知道。basic语言,并不支持直接换行编写代码,代码需要一行写完,写不完的需要

用下划线标注下一行和当前行合并是一行内容,否则就需要将代码写为一行,但显然不符合我

们的习惯,一般一行代码最好限定在80个字符以内,很欣慰的告诉大家,这就是2010中vb.net

的第一个特性,支持直接换行,不再需要下划线来链接了。例如

VB.NET code
Public Sub Test(ByVal name as String,
                ByVal Age as String)
    MessageBox.Show("Name is " & name &
                    "and age is " & age)
End Sub


这段代码,你省去了4个下划线,这个更新非常让人愉悦,因为这意味着vb.net的代码也具有

了灵活性,这以前总是c系列语法才有的特性,其实了解c#和vb.net语法的人都希望看到一个

具有c系列语法的灵活,但是使用更为形象关键字的Basic语法,vb.net已经开始向这个方向发

展了,这真让人开心。


2.对于Lambda提供了更丰富的支持,以前只有Function关键字,现在增加了没有返回值的Sub

关键字。

VB.NET code
Function关键字实例
Dim nums() As Integer = {1, 2, 3, 4, 5}

nums = Array.FindAll(nums, Function(n)
                               Console.WriteLine("testing " & n)
                               Return n > 2
                           End Function)
Sub关键字实例
Array.ForEach(nums, Sub(n)
                        Console.Write("Number: ")
                        Console.WriteLine(n)
                    End Sub)


3.自动实现的属性,面向对象思想中,我们不应该公开自己的成员变量,但如果所有都按这个

思想,使用属性编写,我们增加一个成员变量就需要编写
VB.NET code
Private _FirstName As String

Property FirstName() As String
    Get
        Return _FirstName
    End Get
    Set(ByVal value As String)
        _FirstName = value
    End Set
End Property


在vb.net 2010中,我们不需要这么写了。你只要编写属性就可以了,编译器在编译的时候会

替你增加成员变量。你只需要一行,就和上面效果一样了。
VB.NET code
Property FirstName() As String


另外,可以设置属性的默认值,例如:
VB.NET code
Property FirstName() As String = "George" '中文译音为:饺子


4.初始化数组,使用From{元素}方式给集合对象赋初始值
下面是几种添加方法
VB.NET code
'List
Dim list As New List(Of String) From {"abc", "def", "ghi"}
'Dictionary
Dim dict As New Dictionary(Of Integer, String) From {{1, "abc"},{2, "def"}}

'当然,你也可以不用声明类型,编译器会根据集合的类型来识别
Dim a = {1, 2, 3} '编译器会推断为 Integer()
Dim b = {1, 2, 3.5} '编译器会推断为 Double()
Dim c = {"1", "2", "3"} '编译器会推断为 String()
Dim d = {1, "123"} '编译器会推断为 Object() 如果设置了Option Strict On,会有警告
Dim e = {{1, 2, 3}, {4, 5, 6}} '编译器会推断为 Integer(,)
Dim f = {({1, 2, 3}), ({4, 5, 6})} '编译器会推断为 Integer()()
5.这个功能没看懂,大概意思可能是把com组建内嵌进去,减少发布时候的麻烦?贴原文好了
Compiling without PIAs
Using .NET to program against the Microsoft Office Object Model requires the use of Primary Interop Assemblies (PIAs), which must be deployed to the end-user’s machine. These assemblies are often very large, so deploying them can often be a nuisance.
Visual Basic 10.0 will allow these applications to be deployed without requiring PIAs to exist on the user’s machine. It does this by generating “local types” that perform the interop calls to the COM library directly. These types are annotated by the compiler in such a way that the Common Language Runtime (CLR) can unify these types across assemblies. The compiler will not copy every type in the PIA into your assembly, just the ones you actually use.
In order to turn on this feature, select the reference in Solution Explorer and then set “Embed Interop Tyeps” to “True” in the Properties window: