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
How do I determine the size of my array in C#
In C#, determining the size of an array is accomplished using the Length property. This property returns the total number of elements in the array as an int value.
Syntax
Following is the syntax for getting array length −
arrayName.Length
Parameters
The Length property does not take any parameters and returns an integer representing the number of elements.
Return Value
Returns an int value indicating the total number of elements in the array.
Using Length Property for Single-Dimensional Arrays
For single-dimensional arrays, the Length property provides a direct count of elements −
using System;
public class Demo {
public static void Main(string[] args) {
int[] arr = {6, 3, 8, 4};
Console.WriteLine("Array...");
foreach (int i in arr) {
Console.Write(i + " ");
}
Console.WriteLine("\nSize of Array: " + arr.Length);
}
}
The output of the above code is −
Array... 6 3 8 4 Size of Array: 4
Using Length with Different Array Types
The Length property works with arrays of any data type −
using System;
public class ArrayLengthExample {
public static void Main(string[] args) {
string[] names = {"Alice", "Bob", "Charlie"};
double[] prices = {10.99, 25.50, 8.75, 15.00};
bool[] flags = {true, false, true, false, true};
Console.WriteLine("String array length: " + names.Length);
Console.WriteLine("Double array length: " + prices.Length);
Console.WriteLine("Boolean array length: " + flags.Length);
}
}
The output of the above code is −
String array length: 3 Double array length: 4 Boolean array length: 5
Using Length with Multi-Dimensional Arrays
For multi-dimensional arrays, Length returns the total number of elements across all dimensions −
using System;
public class MultiDimensionalExample {
public static void Main(string[] args) {
int[,] matrix = {{1, 2, 3}, {4, 5, 6}};
int[,,] cube = new int[2, 3, 4];
Console.WriteLine("2D array total elements: " + matrix.Length);
Console.WriteLine("3D array total elements: " + cube.Length);
Console.WriteLine("2D array dimensions: " + matrix.GetLength(0) + "x" + matrix.GetLength(1));
}
}
The output of the above code is −
2D array total elements: 6 3D array total elements: 24 2D array dimensions: 2x3
Comparison of Array Size Methods
| Method | Usage | Returns |
|---|---|---|
Length |
Total elements in array | Total count across all dimensions |
GetLength(dimension) |
Elements in specific dimension | Count for specified dimension only |
Rank |
Number of dimensions | Dimension count (1D, 2D, 3D, etc.) |
Conclusion
The Length property is the most common and straightforward way to determine array size in C#. For single-dimensional arrays, it returns the element count directly, while for multi-dimensional arrays, it returns the total elements across all dimensions.
