C# .Length

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

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

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours

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

All contributors

Learn C# on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
    • Beginner Friendly.
      15 hours