Array Length PropertyUse the Length property on an array to get the size or element count of an array.
This page was last reviewed on Dec 1, 2023.
Length, array. A 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).
Shows an array
Length notes. We see what happens when you get the Length of a one-dimensional array, an empty array, and a null array reference.
Array Count
Initial Length example. Here we access Length on several instances of arrays. We see related properties, such as LongLength and the GetLength method.
Step 1 The program uses the Length property to get the length of a new array. Length has no parentheses, as it is a property.
Step 2 LongLength is the same as Length except it is returned as a long type. It can support bigger arrays too.
Step 3 We get the length of an array with no elements. The Length property returns the value 0. This does not raise an exception.
Step 4 GetLength returns the length of an array at a dimension. We get dimension zero of a one-dimensional array.
Shows an 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);
Two-dimensional length. 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.
Note The Length property, when used on a 2D array, will return the total number of elements, not just a dimension.
Here Length returns 5 * 10 elements, which is 50. Every element is counted in Length.
2D Array
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);
Null array length. This program will raise an exception when you run it. Because the array reference is null, you will get an exception.
null Array
Note You receive "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 error. GetLength 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.
Detail The exception is "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()...
Discussion. Jagged arrays are essentially single-dimensional arrays of single-dimensional arrays. You can access their lengths the same as with one-dimensional arrays.
Initialization. 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.
Array Initialize
Performance. In a tight loop where hoisting the Length check will not affect JIT, you can cache it in a variable for a performance boost.
Tip It is a good idea to always test this. The JIT compiler sometimes generates slower instructions if you cache the length.
Summary. There are many reasons in C# programs to access array lengths. Sometimes exceptions are raised by Length and GetLength.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 1, 2023 (edit).
© 2007-2024 Sam Allen.