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
Total number of elements in a specified dimension of an Array in C#
In C#, you can get the total number of elements in a specific dimension of an array using the GetLongLength() method. This method returns a long value representing the number of elements in the specified dimension, making it useful for both single-dimensional and multi-dimensional arrays.
Syntax
Following is the syntax for using GetLongLength() method −
long count = array.GetLongLength(dimension);
Parameters
dimension − An integer that specifies the dimension (zero-based index) for which you want to get the number of elements.
Return Value
The method returns a long value representing the number of elements in the specified dimension of the array.
Using GetLongLength() with Single-Dimensional Array
Example
using System;
public class Demo {
public static void Main() {
string[] products = new string[] { "Andy", "Mark", "Gary", "Andre" };
Console.WriteLine("Array elements: [" + string.Join(", ", products) + "]");
Console.WriteLine("Number of elements in dimension 0: " + products.GetLongLength(0));
Console.WriteLine("Array rank (number of dimensions): " + products.Rank);
Console.WriteLine("Total elements in array: " + products.Length);
}
}
The output of the above code is −
Array elements: [Andy, Mark, Gary, Andre] Number of elements in dimension 0: 4 Array rank (number of dimensions): 1 Total elements in array: 4
Using GetLongLength() with Multi-Dimensional Array
Example
using System;
public class Demo {
public static void Main() {
string[,,] stdNames = new string[,,] {
{ { "John", "Gary" },
{ "Jacob", "Harry" } },
{ { "Kevin", "Ryan" },
{ "David", "Mark" } }
};
Console.WriteLine("3D Array dimensions:");
Console.WriteLine("Dimension 0 (depth): " + stdNames.GetLongLength(0));
Console.WriteLine("Dimension 1 (rows): " + stdNames.GetLongLength(1));
Console.WriteLine("Dimension 2 (columns): " + stdNames.GetLongLength(2));
Console.WriteLine("Total elements: " + stdNames.Length);
Console.WriteLine("Array rank: " + stdNames.Rank);
}
}
The output of the above code is −
3D Array dimensions: Dimension 0 (depth): 2 Dimension 1 (rows): 2 Dimension 2 (columns): 2 Total elements: 8 Array rank: 3
Comparison with Other Array Methods
| Method | Purpose | Return Type |
|---|---|---|
GetLongLength(dimension) |
Elements in specific dimension | long |
Length |
Total elements in array | int |
GetLength(dimension) |
Elements in specific dimension | int |
Rank |
Number of dimensions | int |
Conclusion
The GetLongLength() method provides an efficient way to determine the number of elements in a specific dimension of an array. It works with both single-dimensional and multi-dimensional arrays, returning a long value that can handle very large arrays better than the integer-based GetLength() method.
