Note: You receive "System.NullReferenceException: Object reference not set to an instance of an object."
C# program that causes null error
static void Main()
int test = null;
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
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.
Text: The exception is "System.IndexOutOfRangeException: Index was outside the bounds of the array."
C# program that causes GetLength error
static void Main()
int test = new int;
Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Array.GetLength(Int32 dimension)
LongLength. The array type offers also the LongLength property. This is only useful when you have an element count greater than the max value of an int.
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.
Discussion. Jagged arrays are essentially single-dimensional arrays of single-dimensional arrays. You can access their lengths the same as with one-dimensional arrays.Jagged Arrays
Count, LINQ. Count() is an extension method that acts on IEnumerable. You can use it to get the array length. It is many times slower and unnecessary for array types.Count
String arrays. Like all arrays, regardless of type, string arrays are of type Array and will have the Length property. Therefore, string arrays work the same as int arrays here.
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.
Default: The default value of elements in an array can be determined with the default operator.