Array.GetLength(Int32) Método
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Obtém um inteiro de 32 bits que representa o número de elementos na dimensão especificada do Array.
public:
int GetLength(int dimension);
public int GetLength (int dimension);
member this.GetLength : int -> int
Public Function GetLength (dimension As Integer) As Integer
Um inteiro de 32 bits que representa o número de elementos na dimensão especificada.
O exemplo a seguir mostra como usar GetLength para exibir as dimensões de duas matrizes com diferentes classificações.
using namespace System;
public ref class SamplesArray
{
public:
static void Main()
{
// make a single dimension array
Array^ MyArray1 = Array::CreateInstance(int::typeid, 5);
// make a 3 dimensional array
Array^ MyArray2 = Array::CreateInstance(int::typeid, 5, 3, 2);
// make an array container
Array^ BossArray = Array::CreateInstance(Array::typeid, 2);
BossArray->SetValue(MyArray1, 0);
BossArray->SetValue(MyArray2, 1);
int i = 0, j, rank;
for each (Array^ anArray 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; j < rank; j++)
{
Console::WriteLine(" Length of dimension({0:d}) = {1:d}", j, anArray->GetLength(j));
}
}
else
{
Console::WriteLine("Lengths of single dimension array[{0:d}]", i);
}
// show the total length of the entire array or all dimensions
Console::WriteLine(" Total length of the array = {0:d}", anArray->Length);
Console::WriteLine();
i++;
}
}
};
int main()
{
SamplesArray::Main();
}
/*
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
*/
using System;
public class SamplesArray
{
public static void Main()
{
// make a single dimension array
Array MyArray1 = Array.CreateInstance(typeof(int), 5);
// make a 3 dimensional array
Array MyArray2 = Array.CreateInstance(typeof(int), 5, 3, 2);
// make an array container
Array BossArray = Array.CreateInstance(typeof(Array), 2);
BossArray.SetValue(MyArray1, 0);
BossArray.SetValue(MyArray2, 1);
int i = 0, j, rank;
foreach (Array anArray 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; j < rank; j++)
{
Console.WriteLine(" Length of dimension({0:d}) = {1:d}", j, anArray.GetLength(j));
}
}
else
{
Console.WriteLine("Lengths of single dimension array[{0:d}]", i);
}
// show the total length of the entire array or all dimensions
Console.WriteLine(" Total length of the array = {0:d}", anArray.Length);
Console.WriteLine();
i++;
}
}
}
/*
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
*/
open System
// make a single dimension array
let myArray1 = Array.zeroCreate<int> 5
// make a 3 dimensional array
let myArray2 = Array3D.zeroCreate<int> 5 3 2
// make an array container
let bossArray: Array [] =
[| myArray1
myArray2 |]
let mutable i = 0
for anArray in bossArray do
let rank = anArray.Rank;
if rank > 1 then
printfn $"Lengths of {rank:d} dimension array[{i:d}]"
// show the lengths of each dimension
for j = 0 to rank - 1 do
printfn $" Length of dimension({j:d}) = {anArray.GetLength(j):d}"
else
printfn $"Lengths of single dimension array[{i:d}]"
// show the total length of the entire array or all dimensions
printfn $" Total length of the array = {anArray.Length:d}\n"
i <- i + 1
// 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
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
Else
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)
Console.WriteLine()
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
Um exemplo é GetLength GetLength(0)
, que retorna o número de elementos na primeira dimensão do Array.
Este método é uma operação O(1).
Produto | Versões |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |