C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: The program uses the Length property to get the length of a new array. Length has no parentheses, as it is a property.
PropertyEmpty: We get the length of an array with no elements. The Length property returns the value 0. This does not raise an exception.
Info: LongLength is the same as Length except it is returned as a long type. It can support bigger arrays too.
GetLength: This returns the length of an array at a dimension. We get dimension zero of a one-dimensional array.
C# program that uses array Length property
using System;
class Program
{
static void Main()
{
// Basic array length example.
int[] arrayA = new int[5];
int lengthA = arrayA.Length;
Console.WriteLine("LENGTH: " + lengthA);
// Long array length example.
long longLength = arrayA.LongLength;
Console.WriteLine("LONGLENGTH: " + longLength);
// Zero length array example.
int[] zero = new int[0];
int lengthZero = zero.Length;
Console.WriteLine("LENGTH: " + lengthZero);
// GetLength 0 example.
int lengthE = arrayA.GetLength(0);
Console.WriteLine("GETLENGTH: " + lengthE);
}
}
Output
LENGTH: 5
LONGLENGTH: 5
LENGTH: 0
GETLENGTH: 5
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 ArrayC# program that uses GetLength, 2D array
using System;
class Program
{
static void Main()
{
// Two-dimensional GetLength example.
int[,] two = new int[5, 10];
Console.WriteLine("GETLENGTH: " + two.GetLength(0)); // Writes 5
Console.WriteLine("GETLENGTH: " + two.GetLength(1)); // Writes 10
// Two-dimensional Length example.
Console.WriteLine("LENGTH: " + two.Length); // Writes 50
}
}
Output
GETLENGTH: 5
GETLENGTH: 10
LENGTH: 50
Note: You receive "System.NullReferenceException: Object reference not set to an instance of an object."
C# program that causes null error
using System;
class Program
{
static void Main()
{
int[] test = null;
Console.WriteLine(test.Length);
}
}
Output
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Program.Main()...
Text: The exception is "System.IndexOutOfRangeException: Index was outside the bounds of the array."
IndexOutOfRangeExceptionC# program that causes GetLength error
using System;
class Program
{
static void Main()
{
int[] test = new int[500];
Console.WriteLine(test.GetLength(1));
}
}
Output
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Array.GetLength(Int32 dimension)
at Program.Main()...
Note: The max value of int is greater than 2 billion. If each array element is a byte, this comes to nearly 2 gigabytes of memory.
And: Depending on your system configuration, it may be impossible to allocate an array this large.
Default: The default value of elements in an array can be determined with the default operator.
DefaultTip: It is a good idea to always test this. The JIT compiler sometimes generates slower instructions if you cache the length.
Also: The LongLength property, which returns a long number, has no performance advantage.