共用方式為


Visual Studio 2012 中 Visual Basic 的重大變更

下列表格列出可能造成應用程式無法編譯的 Visual Basic 2010 建立在 Visual Studio 2012 中的 Visual Basic ,並變更可能變更應用程式的執行階段行為的變更。

分類

問題

描述

型別推斷

在運算元為陣列常值的 return 陳述式,執行階段陣列型別是從函式簽章來決定的而不是從陣列常值推斷。

這項變更置於您無法讓您已傳回陣列常值,,如下列範例所示:

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 Test 造成多載解析編譯時期錯誤。 在下列範例中, Process(theList) 行在 Visual Studio Test 會導致編譯時期錯誤。 在目前的版本,行符合 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 使用者入門

非中斷語言內建何時中斷?