C# Array Length Property

Every array has a length. In the C# language we access the Length property on a non-null array. We see what happens when you get the Length of a one-dimensional array, an empty array, and a null array reference.

Illustration

Example

To start, we see examples of accessing Length on several instances of arrays. We also see related properties, such as LongLength and the GetLength method. Usually, Length is the most important.

This C# example program uses the Length property on an array. Length returns the array size.

Program that uses array Length [C#]

using System;

class Program
{
    static void Main()
    {
	// A
	// Basic array length example.
	int[] arrayA = new int[5];
	int lengthA = arrayA.Length;
	Console.WriteLine(lengthA); // Writes 5

	// B
	// Long array length example.
	long longLength = arrayA.LongLength;
	Console.WriteLine(longLength); // Writes 5

	// C
	// Zero length array example.
	int[] zero = new int[0];
	int lengthZero = zero.Length;
	Console.WriteLine(lengthZero); // Writes 0

	// D
	// Null array length example exception.
	// int[] dead = null;
	// Console.WriteLine(dead.Length);

	// E
	// GetLength 0 example.
	int lengthE = arrayA.GetLength(0);
	Console.WriteLine(lengthE); // Writes 5

	// F
	// GetLength 1 example exception.
	// int lengthF = arrayA.GetLength(1);
	// Console.WriteLine(lengthF);

	// G
	// Two-dimensional GetLength example.
	int[,] two = new int[5, 10];
	Console.WriteLine(two.GetLength(0)); // Writes 5
	Console.WriteLine(two.GetLength(1)); // Writes 10

	// H
	// Two-dimensional Length example.
	Console.WriteLine(two.Length); // Writes 50
    }
}

Output

5
5
0
5
5
10
50
Letters of the alphabet: ABC

Description of part A. This is the first section and it shows how you can use the Length property to get the length of a new array. Length has no parentheses, as it is a property. You cannot assign Length.

Property Examples

Description of part B. This shows how you can use the LongLength property on an array. You will most likely not need to use this.

Description of part C. Here we get the length of an array with no elements. The Length property returns the value 0. This doesn't raise an exception as the array is not null.

Null keyword

Description of part D. This example is commented out, as it will raise an exception when you run it. Because the array reference is null, you will get an exception. (Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.)

NullReferenceException and Null Parameter

Description of part E. Here we see the GetLength method, which returns the length of an array at a dimension. Here we get dimension zero of a one-dimensional array. This results in the same value as Length.

Warning

Description of part F. Here we see the GetLength method, but it raises an exception. This is because the array used has one dimension, but we asked for the second dimension. (Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.)

IndexOutOfRangeException

Description of part G. Here we see a simple example of using GetLength on a two-dimensional array. First, the example shows the length of the first dimension. Next, it shows the second dimension.

Description of part H. Finally we see that the Length property, when used on a two-dimensional array, will return the total number of elements, not just a dimension. Here it returns 5 x 10 elements, which is 50.

Questions

Question and answer

At this point, you may have some questions about the Length property on arrays. Let's address them in the paragraphs that follow.

What about jagged array lengths? Jagged arrays are essentially single-dimensional arrays of single-dimensional arrays. You can access their lengths the same as with one-dimensional arrays. Their declaration syntax differs.

Jagged Array Examples

What about Count() from LINQ? This is an extension method that acts on IEnumerable. You can use it to get the array length. However, it is many times slower and unnecessary for array types.

Count Array Elements

What about string arrays? All arrays, regardless of type, are of type Array and will have the Length property. Therefore, string arrays work the same as int arrays here.

String Array

Is the Length property fast? Yes, in general. However, in a really tight loop where hoisting the Length check will not affect JIT, you can cache it in a variable for a performance boost. Always test this, however, as the JIT compiler can sometimes degrade if you cache the length.

Does it matter if the array elements are initialized? No. You can get the length of any allocated array, regardless of whether the elements have been assigned. Int array elements are initialized to 0. This is not the same as a null array.

Is LongLength slower? Without benchmarking it carefully, it is almost certainly slower. In IL Disassembler, we can see that it has more IL instructions. Additionally, Int64 types are generally slower than Int32 types.

IL Disassembler Tutorial

Summary

The C# programming language

We saw lots of examples of different array instances and their Length properties. Further, we looked at the exceptions raised by Length and GetLength. This is useful for debugging purposes. Finally, we discussed the finer points of array lengths.

Array Types
.NET