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).
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.
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
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.
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.
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.