Export (0) Print
Expand All

Array.GetLength Method

Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Public Function GetLength ( _
	dimension As Integer _
) As Integer


Type: System.Int32

A zero-based dimension of the Array whose length needs to be determined.

Return Value

Type: System.Int32
A 32-bit integer that represents the number of elements in the specified dimension.


dimension is less than zero.


dimension is equal to or greater than Rank.

An example of GetLength is GetLength(0), which returns the number of elements in the first dimension of the Array.

This method is an O(1) operation.

The following example shows how to use GetLength to display the dimensions of two arrays with different ranks.

Imports System

Public Class SamplesArray
    Public Shared Sub Main()
        ' make a single dimension array 
        Dim MyArray1 As Array = Array.CreateInstance(GetType(Integer), 5)

        ' make a 3 dimensional array 
        Dim MyArray2 As Array = Array.CreateInstance(GetType(Integer), 5, 3, 2)

        ' make an array container 
        Dim BossArray As Array = Array.CreateInstance(GetType(Array), 2)
        BossArray.SetValue(MyArray1, 0)
        BossArray.SetValue(MyArray2, 1)

        Dim i As Integer = 0
        Dim j As Integer 
        Dim rank As Integer 
        For Each anArray As Array In BossArray
            rank = anArray.Rank
            If rank > 1
                Console.WriteLine("Lengths of {0:d} dimension array[{1:d}]", rank, i)
                ' show the lengths of each dimension 
                For j = 0 To rank - 1
                    Console.WriteLine("    Length of dimension({0:d}) = {1:d}", j, anArray.GetLength(j))
                Next j
                Console.WriteLine("Lengths of single dimension array[{0:d}]", i)
            End If 
            ' show the total length of the entire array or all dimensions
            Console.WriteLine("    Total length of the array = {0:d}", anArray.Length)
            i = i + 1
        Next anArray
    End Sub 
End Class 

'This code produces the following output: 

'Lengths of single dimension array[0] 
'    Total length of the array = 5 

'Lengths of 3 dimension array[1] 
'    Length of dimension(0) = 5 
'    Length of dimension(1) = 3 
'    Length of dimension(2) = 2 
'    Total length of the array = 30

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft