영어로 읽기

다음을 통해 공유


Array.GetLength(Int32) 메서드

정의

지정된 차원의 Array에 있는 요소의 수를 나타내는 32비트 정수를 가져옵니다.

public int GetLength (int dimension);

매개 변수

dimension
Int32

길이를 지정해야 하는 Array 의 0부터 시작하는 차원입니다.

반환

Int32

지정된 차원의 요소 수를 나타내는 32비트 정수입니다.

예외

dimension가 0보다 작은 경우

또는

dimensionRank보다 크거나 같은 경우

예제

다음 예제에서는 서로 다른 순위를 가진 두 배열의 차원을 표시하는 데 사용하는 GetLength 방법을 보여 줍니다.

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
*/

설명

GetLength 예는 GetLength(0)1 차원의 요소 수를 반환하는 Array것입니다.

이 방법은 O(1) 작업에 설명 합니다.

적용 대상

추가 정보