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.Length property of array class do in C#?
The Array.Length property in C# is used to get the total number of elements in an array. This property returns an integer value representing the array's size, which is essential for array manipulation and iteration.
Syntax
Following is the syntax for using the Array.Length property −
int length = arrayName.Length;
Return Value
The Length property returns an int value representing the total number of elements in the array.
Using Array.Length with Array.CreateInstance
The following example demonstrates how to use Array.Length with dynamically created arrays −
using System;
class Program {
static void Main(string[] args) {
Array arr = Array.CreateInstance(typeof(String), 3);
arr.SetValue("Electronics", 0);
arr.SetValue("Clothing", 1);
arr.SetValue("Appliances", 2);
Console.WriteLine("Length: " + arr.Length);
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine("Element at index " + i + ": " + arr.GetValue(i));
}
}
}
The output of the above code is −
Length: 3 Element at index 0: Electronics Element at index 1: Clothing Element at index 2: Appliances
Using Array.Length with Regular Arrays
The Length property works with all array types, including regular arrays declared using square brackets −
using System;
class Program {
static void Main(string[] args) {
int[] numbers = {10, 20, 30, 40, 50};
string[] colors = {"Red", "Green", "Blue"};
Console.WriteLine("Numbers array length: " + numbers.Length);
Console.WriteLine("Colors array length: " + colors.Length);
Console.WriteLine("\nNumbers array elements:");
for (int i = 0; i < numbers.Length; i++) {
Console.WriteLine("Index " + i + ": " + numbers[i]);
}
}
}
The output of the above code is −
Numbers array length: 5 Colors array length: 3 Numbers array elements: Index 0: 10 Index 1: 20 Index 2: 30 Index 3: 40 Index 4: 50
Using Array.Length for Multi-dimensional Arrays
For multi-dimensional arrays, Length returns the total number of elements across all dimensions −
using System;
class Program {
static void Main(string[] args) {
int[,] matrix = {{1, 2, 3}, {4, 5, 6}};
int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[3] {1, 2, 3};
jaggedArray[1] = new int[2] {4, 5};
Console.WriteLine("2D array total elements: " + matrix.Length);
Console.WriteLine("Jagged array length (rows): " + jaggedArray.Length);
Console.WriteLine("First row length: " + jaggedArray[0].Length);
Console.WriteLine("Second row length: " + jaggedArray[1].Length);
}
}
The output of the above code is −
2D array total elements: 6 Jagged array length (rows): 2 First row length: 3 Second row length: 2
Common Use Cases
-
Loop iteration: Using
array.Lengthas the loop condition prevents index out of bounds exceptions. -
Array validation: Checking if an array is empty by comparing
Lengthto zero. -
Dynamic operations: Resizing or copying arrays based on their current length.
Conclusion
The Array.Length property is a fundamental tool for working with arrays in C#. It provides the total count of elements in any array type and is essential for safe array iteration and manipulation operations.
