Length
, arrayA C# array has a length—this is its size (its element count). We access the Length
property. An int
of 0 or greater is returned—no iteration is done (a cache is used).
Length
notesWe see what happens when you get the Length
of a one-dimensional array, an empty array, and a null
array reference.
Length
exampleHere we access Length
on several instances of arrays. We see related properties, such as LongLength
and the GetLength
method.
Length
property to get the length of a new array. Length
has no parentheses, as it is a property.LongLength
is the same as Length
except it is returned as a long type. It can support bigger arrays too.Length
property returns the value 0. This does not raise an exception.GetLength
returns the length of an array at a dimension. We get dimension zero of a one-dimensional array.using System; // Step 1: basic array length example. string[] arrayA = new string[] { "cat", "apple", "frog" }; int lengthA = arrayA.Length; Console.WriteLine("LENGTH: " + lengthA); // Step 2: long array length example. long longLength = arrayA.LongLength; Console.WriteLine("LONGLENGTH: " + longLength); // Step 3: zero length array example. int[] zero = new int[0]; int lengthZero = zero.Length; Console.WriteLine("LENGTH: " + lengthZero); // Step 4: GetLength 0 example. int lengthE = arrayA.GetLength(0); Console.WriteLine("GETLENGTH: " + lengthE);LENGTH: 3 LONGLENGTH: 3 LENGTH: 0 GETLENGTH: 3
Here we see a simple example of using GetLength
on a two-dimensional array. GetLength
receives a rank (0 or 1) and prints the result size.
Length
property, when used on a 2D array, will return the total number of elements, not just a dimension.Length
returns 5 * 10 elements, which is 50. Every element is counted in Length
.using System; // Two-dimensional GetLength example. int[,] two = new int[5, 10]; Console.WriteLine("GETLENGTH: " + two.GetLength(0)); Console.WriteLine("GETLENGTH: " + two.GetLength(1)); // Two-dimensional Length example. Console.WriteLine("LENGTH: " + two.Length);GETLENGTH: 5 GETLENGTH: 10 LENGTH: 50
This program will raise an exception when you run it. Because the array reference is null
, you will get an exception.
System.NullReferenceException
: Object reference not set to an instance of an object."using System; class Program { static void Main() { int[] test = null; Console.WriteLine(test.Length); } }Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Program.Main()...
Index
errorGetLength
can cause an exception when we access a dimension that is not part of the array. Here the array has one dimension, but we ask for the second dimension.
System.IndexOutOfRangeException
: Index
was outside the bounds of the array."using System; class Program { static void Main() { int[] test = new int[500]; Console.WriteLine(test.GetLength(1)); } }Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Array.GetLength(Int32 dimension) at Program.Main()...
Jagged arrays are essentially single-dimensional arrays of single-dimensional arrays. You can access their lengths the same as with one-dimensional arrays.
It does not matter if the array elements are initialized. You can get the length of any allocated array. Int
array elements are initialized to 0.
In a tight loop where hoisting the Length
check will not affect JIT, you can cache it in a variable for a performance boost.
There are many reasons in C# programs to access array lengths. Sometimes exceptions are raised by Length
and GetLength
.