Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does Array.LongLength property of array class do in C#?
The Array.LongLength property in C# gets a 64-bit integer (long) that represents the total number of elements in all dimensions of an array. This property is particularly useful when working with very large arrays that might exceed the range of a 32-bit integer.
Syntax
Following is the syntax for using the LongLength property −
long totalElements = arrayName.LongLength;
Return Value
The property returns a long value representing the total number of elements across all dimensions of the array.
Single-Dimensional Array Example
For a single-dimensional array, LongLength returns the same value as Length but as a long type −
using System;
class Program {
static void Main() {
int[] arr = new int[10];
Console.WriteLine("Length: " + arr.Length);
Console.WriteLine("LongLength: " + arr.LongLength);
Console.WriteLine("Type of Length: " + arr.Length.GetType());
Console.WriteLine("Type of LongLength: " + arr.LongLength.GetType());
}
}
The output of the above code is −
Length: 10 LongLength: 10 Type of Length: System.Int32 Type of LongLength: System.Int64
Multi-Dimensional Array Example
For multi-dimensional arrays, LongLength calculates the total elements by multiplying all dimension lengths −
using System;
class Program {
static void Main() {
long[,] arr1 = new long[15, 35];
long len1 = arr1.GetLongLength(0);
long len2 = arr1.GetLongLength(1);
Console.WriteLine("Dimension 0 length: " + len1);
Console.WriteLine("Dimension 1 length: " + len2);
Console.WriteLine("Total elements (LongLength): " + arr1.LongLength);
Console.WriteLine("Calculation: " + len1 + " × " + len2 + " = " + (len1 * len2));
}
}
The output of the above code is −
Dimension 0 length: 15 Dimension 1 length: 35 Total elements (LongLength): 525 Calculation: 15 × 35 = 525
Comparison with Length Property
| Property | Return Type | Maximum Value | Use Case |
|---|---|---|---|
| Length | int | 2,147,483,647 | Regular arrays |
| LongLength | long | 9,223,372,036,854,775,807 | Very large arrays |
Three-Dimensional Array Example
The property works with arrays of any number of dimensions −
using System;
class Program {
static void Main() {
int[,,] arr3D = new int[5, 4, 3];
Console.WriteLine("3D Array dimensions:");
Console.WriteLine("Dimension 0: " + arr3D.GetLongLength(0));
Console.WriteLine("Dimension 1: " + arr3D.GetLongLength(1));
Console.WriteLine("Dimension 2: " + arr3D.GetLongLength(2));
Console.WriteLine("Total elements: " + arr3D.LongLength);
}
}
The output of the above code is −
3D Array dimensions: Dimension 0: 5 Dimension 1: 4 Dimension 2: 3 Total elements: 60
Conclusion
The Array.LongLength property returns the total number of elements in an array as a 64-bit integer, making it essential for very large arrays that exceed the 32-bit integer range. It multiplies all dimension lengths to calculate the total element count across all dimensions of the array.
