陣列轉換 (Visual Basic)Array Conversions (Visual Basic)

您可以將陣列型別轉換成不同的陣列型別,提供符合下列條件:You can convert an array type to a different array type provided you meet the following conditions:

  • 相等的順位。Equal Rank. 兩個陣列的陣序規範必須相同,也就是它們必須具有相同的維度數目。The ranks of the two arrays must be the same, that is, they must have the same number of dimensions. 不過,個別的維度的長度不需要相同。However, the lengths of the respective dimensions do not need to be the same.

  • 項目資料型別。Element Data Type. 兩個陣列元素的資料類型必須是參考型別。The data types of the elements of both arrays must be reference types. 您無法轉換Integer陣列Long陣列、 或甚至Object陣列,因為牽涉到至少一個實值型別。You cannot convert an Integer array to a Long array, or even to an Object array, because at least one value type is involved. 如需詳細資訊,請參閱 Value Types and Reference TypesFor more information, see Value Types and Reference Types.

  • 可轉換性。Convertibility. 轉換,以擴大或縮小,必須能夠在兩個陣列項目類型之間。A conversion, either widening or narrowing, must be possible between the element types of the two arrays. 舉例來說,未通過這項需求是嘗試之間的轉換String陣列和陣列的類別衍生自System.AttributeAn example that fails this requirement is an attempted conversion between a String array and an array of a class derived from System.Attribute. 這兩種類型沒有任何共通點,並沒有任何類型的轉換之間存在。These two types have nothing in common, and no conversion of any kind exists between them.

擴展或縮小取決於個別的項目轉換是否會擴大或縮小到另一個陣列類型的轉換。A conversion of one array type to another is widening or narrowing depending on whether the conversion of the respective elements is widening or narrowing. 如需詳細資訊,請參閱 Widening and Narrowing ConversionsFor more information, see Widening and Narrowing Conversions.

轉換成物件陣列Conversion to an Object Array

當您宣告Object但不初始化,其項目類型的陣列是Object,只要它會保持未初始化。When you declare an Object array without initializing it, its element type is Object as long as it remains uninitialized. 當您設定特定類別的陣列時,它會在該類別的型別。When you set it to an array of a specific class, it takes on the type of that class. 不過,其基礎類型仍Object,以及您可以接著將它設定為不相關的類別的另一個陣列。However, its underlying type is still Object, and you can subsequently set it to another array of an unrelated class. 因為所有的類別衍生自Object,您可以從任何類別變更陣列的項目類型,與任何其他類別。Since all classes derive from Object, you can change the array's element type from any class to any other class.

在下列範例中,沒有任何轉換存在型別之間studentString,但都是衍生自Object,因此所有指派都都有效。In the following example, no conversion exists between types student and String, but both derive from Object, so all assignments are valid.

' Assume student has already been defined as a class.  
Dim testArray() As Object  
' testArray is still an Object array at this point.  
Dim names() As String = New String(3) {"Name0", "Name1", "Name2", "Name3"}  
testArray = New student(3) {}  
' testArray is now of type student().  
testArray = names  
' testArray is now a String array.  

陣列的基礎類型Underlying Type of an Array

如果您原本宣告具有特定類別的陣列,其基礎的項目類型會是該類別。If you originally declare an array with a specific class, its underlying element type is that class. 如果您接著將它設定為另一個類別的陣列,必須有兩個類別之間的轉換。If you subsequently set it to an array of another class, there must be a conversion between the two classes.

在下列範例中,studentsstudent陣列。In the following example, students is a student array. 因為沒有任何轉換存在之間Stringstudent,最後一個陳述式會失敗。Since no conversion exists between String and student, the last statement fails.

Dim students() As student  
Dim names() As String = New String(3) {"Name0", "Name1", "Name2", "Name3"}  
students = New Student(3) {}  
' The following statement fails at compile time.  
students = names  

另請參閱See also