Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Array.Length — array element count

Array.Length returns an array's element count as int. For a multidimensional array it covers every dimension, and for an empty array it is 0.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

public int Length { get; }

array.Length

Array.Length is a read-only property: use array.Length, without invocation parentheses. Reading it is an O(1) operation.

Length means the number of element slots, not the number of nonzero values. An array has a fixed size: new int[3] has length 3, even though it initially contains only zeros.

For new int[2, 3], Length is 6. Do not confuse it with Rank, the number of dimensions. GetLength(0) and GetLength(1) return the individual dimension sizes: 2 and 3.

If a multidimensional array contains more than Int32.MaxValue elements, reading Length throws OverflowException. LongLength exposes the total element count as a 64-bit integer.

Examples

using System;

int[] values = new int[3];
int[,] grid = new int[2, 3];

Console.WriteLine(values.Length);
Console.WriteLine(grid.Length);
Console.WriteLine(grid.GetLength(0));
Console.WriteLine(grid.GetLength(1));
Console.WriteLine(new int[0].Length);

Result

3
6
2
3
0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources