本文整理匯總了VB.NET中System.Array.Length屬性的典型用法代碼示例。如果您正苦於以下問題:VB.NET Array.Length屬性的具體用法?VB.NET Array.Length怎麽用?VB.NET Array.Length使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類System.Array
的用法示例。
在下文中一共展示了Array.Length屬性的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。
示例1: Example
Module Example
Public Sub Main()
' Declare a single-dimensional string array
Dim array1d() As String = {"zero", "one", "two", "three"}
ShowArrayInfo(array1d)
' Declare a two-dimensional string array
Dim array2d(,) As String = {{"zero", "0"}, {"one", "1"},
{"two", "2"}, {"three", "3"},
{"four", "4"}, {"five", "5"}}
ShowArrayInfo(array2d)
' Declare a three-dimensional integer array
Dim array3d(,,) As Integer = New Integer(,,) {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}}
ShowArrayInfo(array3d)
End Sub
Private Sub ShowArrayInfo(arr As Array)
Console.WriteLine("Length of Array: {0,3}", arr.Length)
Console.WriteLine("Number of Dimensions: {0,3}", arr.Rank)
' For multidimensional arrays, show number of elements in each dimension.
If arr.Rank > 1 Then
For dimension As Integer = 1 To arr.Rank
Console.WriteLine(" Dimension {0}: {1,3}", dimension,
arr.GetUpperBound(dimension - 1) + 1)
Next
End If
Console.WriteLine()
End Sub
End Module
輸出:
Length of Array: 4 Number of Dimensions: 1 Length of Array: 12 Number of Dimensions: 2 Dimension 1: 6 Dimension 2: 2 Length of Array: 12 Number of Dimensions: 3 Dimension 1: 2 Dimension 2: 2 Dimension 3: 3
示例2: Tester
Module Tester
Sub Main()
Dim i As Integer
Dim array As Integer() ' declare array variable
array = New Integer(9) {} ' allocate memory for array
Console.WriteLine("Subscript " & vbTab & "Value")
For i = 0 To array.GetUpperBound(0)
Console.WriteLine( i & vbTab & array(i))
Next
Console.WriteLine("The array contains " & _
array.Length & " elements.")
End Sub
End Module