Visual Studio 2012 中 Visual Basic 的重大更改

下表列出了可能阻止应用程序在从生成的 Visual Basic 2010 是在 Visual Studio 2012 中的 Visual Basic,并更改可能更改应用程序的运行时行为的更改。

类别

问题

描述

类型推理

该操作数为数组文本的返回语句,运行时数组类型从函数签名确定而不是从数组文本值推断。

此更改在之前无法的放置可以返回一个数组文本,如下面的示例所示:

Function GetArrayLiteral(ByVal number As Integer) As Integer()
    If number < 0 Then
        ' In Visual Studio 2010, this array literal is
        ' is inferred to be an array of objects, which
        ' cannot be converted to an array of integers. 
        ' In the current version, this array literal is
        ' inferred to be an array of integers, which is
        ' the return type of the function. 
        Return {}
    Else
        Return {number}
    End If
End Function

此更改可能导致数组的文本的运行时类型宽相比,它是在 Visual Basic 2010,如下面的示例所示:

Private Sub ArrayLiterals()
    Dim theArray = GetArray()
    Console.WriteLine(theArray.GetType().FullName) 
End Sub

Private Function GetArray() As Object()
    Return {"chromatic", "infinitesimal"}
End Function

' Output in the current version:
'   System.Object[]
' Output in Visual Studio 2010:
'   System.String[]

Lambda 表达式

在 For Each 表达式,则在 lambda 表达式现在可以使用控制变量。

如下面的示例所示,在 lambda 表达式中使用一个 For Each 迭代变量不再导致编译时警告和不再有意外的结果:

Dim methods As New List(Of Action)
For Each word In {"hello", "world"}
    methods.Add(Sub() Console.Write(word & " "))
Next
methods(0)() 
methods(1)() 

' Output in the current version: 
'   hello world
' Output in Visual Studio 2010: 
'   world world

LINQ 表达式

在 For Each 表达式,则在 LINQ 表达式现在可以使用控制变量。

如下面的示例所示,使用在 LINQ 表达式的一个 For Each 迭代变量不再导致编译时警告和不再有意外的结果:

Dim lines As New List(Of IEnumerable(Of String)) 

For Each number In {1, 2, 3}
    Dim line = From letter In {"a", "b", "c"}
            Select number.ToString & letter

    lines.Add(line) 
Next

For Each line In lines
    For Each entry In line
        Console.Write(entry & " ")
    Next
    Console.WriteLine()
Next

' Output in the current version: 
'  1a 1b 1c
'  2a 2b 2c
'  3a 3b 3c

' Output in Visual Studio 2010: 
'  3a 3b 3c
'  3a 3b 3c
'  3a 3b 3c

重载决策

如果泛型类型参数匹配的两个重载同样好地调用方,但一个重载是更具体的,请使用更具体的重载。

此条件在 Visual Studio 2010 导致重载决策编译时错误。在下面的示例中,Process(theList) 行在 Visual Studio 2010 会导致编译时错误。在当前版本中,行与 Process 方法的更为具体的重载。

Private Sub Test()
    Dim theList As New List(Of Integer)
    Process(theList)
    Dim theQueue As New Queue(Of Integer)
    Process(theQueue)
End Sub
Private Sub Process(Of T)(ByVal theList As List(Of T))
    Debug.WriteLine("first overload")
End Sub
Private Sub Process(Of T)(ByVal x As T)
    Debug.WriteLine("second overload")
End Sub
' Output:
'   first overload
'   second overload

请参见

参考

For Each...Next 语句 (Visual Basic)

概念

Lambda 表达式 (Visual Basic)

Visual Studio 2012 中 Visual Basic 的新增功能

其他资源

数组 (Visual Basic)

Visual Basic 中的 LINQ

Visual Basic 入门

一种不中断的语言解决时中断?