Visual Basic 2008 的重大變更
更新: 2008 年 7 月
下列表格列出可能造成在 Visual Studio 2005 中建立的應用程式,無法在 Visual Studio 2008 中進行編譯,或導致執行階段行為改變的變更。
類別 |
變更 |
說明 |
---|---|---|
可為 Null 的型別 |
T 擴展為 T? 在 Visual Studio 2008 中成為預先定義的轉換。 |
在 Visual Studio 2005 中,您可以建立使用者定義的轉換,讓 T 能夠擴展為 Nullable(Of T)。在 Visual Studio 2008 中這項轉換是預先定義的,包含在對可為 Null 的型別的新增支援中。 因為多載解析 (Overload Resolution) 會同時考慮預先定義與使用者定義的轉換,如果兩者都存在時,可能造成模稜兩可 (Ambiguity) 的情況。因此在 Visual Studio 2008 中,當程式碼包含由 T 轉換至 T? 的使用者定義擴充轉換時,可能會造成模稜兩可的情況。 例如,在下列程式碼中,對 c3.Sub1 的呼叫可以在 Visual Studio 2005 中正常運作,但卻會在 Visual Studio 2008 中造成編譯器錯誤。
若要解決這個問題,您可以移除使用者定義的轉換,或明確轉型成 Class1 或 Class2:
|
多載解析 |
在泛型與非泛型類別間的多載解析差異已更正。 |
在 Visual Studio 2005 中有一個問題,會造成泛型類別的多載解析行為和非泛型類別的行為不同。在下列範例中,除了在 Class2 上定義了泛型參數以外,Class1 和 Class2 完全一模一樣。在 Main 中對 c1.Sub1 的呼叫模稜兩可,因為傳遞的引數可以繫結至 Class1 中 Sub1 的任一個多載。這樣模稜兩可的情況,在 Visual Studio 2005 和 Visual Studio 2008 中都會造成編譯器錯誤。 對 c2.Sub1 的呼叫應該要產生一樣的錯誤,但在 Visual Studio 2005 中並不會產生錯誤。相反地,方法會繫結至 Class2 中 Sub1 的不受限制多載。 這個問題在 Visual Studio 2008 中已修正。兩種呼叫現在都會因為模稜兩可而產生編譯器錯誤。這在下列程式碼中說明:
若要解決這個問題,您可以變更多載而不再模棱兩可,或者明確指定型別引數:
|
多載解析 |
使用泛型和 ParamArray 參數的多載解析,在 Visual Studio 2008 中會產生不一樣的結果。 |
多載解析的目的,是嘗試選取其參數與傳遞給方法的引數型別最緊密符合的候選方法。在本節的範例中,Sub1 跨繼承階層架構多載,並受到型別 Integer 和 Short 的引數呼叫。 在 Visual Studio 2005 和 Visual Studio 2008 中的多載解析規則,都指出直接相符優於需要 ParamArray 參數的相符情況。然而,使用 Visual Studio 2005 多載解析規則時,在衍生類別 Class2 中之候選多載的型別推論會失敗,因為 Y 不能同時為 Integer 和 Short,而且需要完全相符。如果引數 sh 和 n 型別相同,會偏向使用 Class2 中的 Sub1,而不會使用在 Class1 中,具有 ParamArray 參數的候選項目。不過,因為兩項引數的型別不同,因此反而會選取 Class1 中的基底類別多載。T 會推論為 Integer,而 Short 會擴展為 ParamArrayp。 Visual Studio 2008 使用新的演算法,會和 Visual Studio 2005 選取一樣的多載,只有在這個特別案例中不同。在 Visual Studio 2008 中,演算法會將 Integer 決定為本範例中的主型別,因為 Short 會擴展至 Integer。衍生類別中的型別參數 Y 會推論為 Integer,且 Short 會擴展至 Integer,因此會呼叫衍生類別中的 Sub1。
您可以在呼叫 Sub1 前將變數 c2 轉型為型別 C1ass1,或傳遞參數 sh 做為陣列,強迫指定 Visual Studio 2005 的行為。
|
請參閱
概念
參考
變更記錄
日期 |
記錄 |
原因 |
---|---|---|
2008 年 7 月 |
加入主題。 |
資訊加強。 |