.Length

nikolaspoczekaj's avatar
Published Nov 3, 2022Updated May 24, 2023
Contribute to Docs

The .Length property returns the total number of elements in the array, including all dimensions of the array.

Syntax

int myLength = myArray.Length;

myArray.Length returns an int that represents the number of elements in myArray.

Example

The following example initializes an array, then prints out the number of elements it contains:

using System;
public class Example
{
public static void Main(string[] args)
{
int[] myArray = {0, 1, 2, 3, 4};
Console.WriteLine(myArray.Length); // Output: 5
}
}

Codebyte Example

The following runnable code uses the .Length property to return the length of the numbers array:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn C# on Codecademy