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 present in an array in C#
In C#, you can get the total number of elements present in an array using several different approaches. The most common methods are using the Length property, GetLength() method, or LongLength property for very large arrays.
Syntax
Following are the different ways to get array length −
// Using Length property (most common) int count = arrayName.Length; // Using GetLength() method for specific dimension int count = arrayName.GetLength(0); // Using LongLength for very large arrays long count = arrayName.LongLength;
Using Length Property
The Length property is the most commonly used approach to get the total number of elements in a single-dimensional array −
using System;
public class Demo {
public static void Main() {
string[] products = { "Electronics", "Accessories", "Clothing", "Toys", "Furniture" };
Console.WriteLine("Products list:");
foreach(string product in products) {
Console.WriteLine(product);
}
Console.WriteLine("Total elements using Length property: " + products.Length);
}
}
The output of the above code is −
Products list: Electronics Accessories Clothing Toys Furniture Total elements using Length property: 5
Using GetLength() Method
The GetLength() method is particularly useful for multidimensional arrays, where you can specify the dimension index −
using System;
public class Demo {
public static void Main() {
// Single-dimensional array
int[] numbers = { 10, 20, 30, 40, 50 };
Console.WriteLine("Single-dimensional array length: " + numbers.GetLength(0));
// Multidimensional array
int[,] matrix = { {1, 2, 3}, {4, 5, 6} };
Console.WriteLine("Matrix rows: " + matrix.GetLength(0));
Console.WriteLine("Matrix columns: " + matrix.GetLength(1));
Console.WriteLine("Total elements in matrix: " + matrix.Length);
}
}
The output of the above code is −
Single-dimensional array length: 5 Matrix rows: 2 Matrix columns: 3 Total elements in matrix: 6
Handling Empty Arrays
When working with empty arrays, all methods return zero −
using System;
public class Demo {
public static void Main() {
string[] emptyArray = new string[] { };
int[] numbers = new int[0];
Console.WriteLine("Empty string array length: " + emptyArray.Length);
Console.WriteLine("Empty int array length: " + numbers.Length);
Console.WriteLine("Using GetLength(0): " + emptyArray.GetLength(0));
Console.WriteLine("Is array fixed size? " + emptyArray.IsFixedSize);
Console.WriteLine("Is array read only? " + emptyArray.IsReadOnly);
}
}
The output of the above code is −
Empty string array length: 0 Empty int array length: 0 Using GetLength(0): 0 Is array fixed size? True Is array read only? False
Comparison of Methods
| Method | Use Case | Return Type |
|---|---|---|
| Length | Single-dimensional arrays (most common) | int |
| GetLength(dimension) | Multi-dimensional arrays, specific dimension | int |
| LongLength | Very large arrays (rare usage) | long |
Conclusion
The Length property is the most efficient and commonly used method to get the total number of elements in a C# array. For multidimensional arrays, use GetLength(dimension) to get the size of specific dimensions, while Length gives the total element count across all dimensions.
