Find length of an array in C#
This post will discuss how to find the length of an array in C#.
1. Using Array.Length Property
The standard solution to find the length of an array is using the Array.Length property. It returns the total number of elements contained in an array. If the array is empty, it returns zero. The following example demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int length = array.Length; Console.WriteLine("Length is " + length); } } |
Output:
Length is 5
For multi-dimensional arrays, the Array.Length property returns the total number of elements in all the dimensions. In other words, it returns the sum of the total number of elements in each dimension of a multidimensional array.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int[,] array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; int length = array.Length; Console.WriteLine("Length is " + length); } } |
Output:
Length is 6
In C#, a jagged array can be of different dimensions and sizes. For a jagged array, the Length property will indicate the number of dimensions in the array. For example,
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; public class Example { public static void Main() { int[][] array = new int[3][]; array[0] = new int[] {1, 2}; array[1] = new int[] {3, 4, 5}; array[2] = new int[] {6, 7, 8, 9}; Console.WriteLine("array.Length is " + array.Length); Console.WriteLine("array[0].Length is " + array[0].Length); Console.WriteLine("array[1].Length is " + array[1].Length); Console.WriteLine("array[2].Length is " + array[2].Length); } } |
Output:
array.Length is 3
array[0].Length is 2
array[1].Length is 3
array[2].Length is 4
2. Using Array.GetLength(Int32) Method
Alternatively, you can use the Array.GetLength() method to get the length of a single-dimensional array. The idea is to pass the zero dimension to the GetLength method to determine the total number of elements in the first dimension of the array.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int length = array.GetLength(0); Console.WriteLine("Length is " + length); } } |
Output:
Length is 5
If the array is multidimensional, the Array.GetLength() method returns the total number of elements in the specified dimension of the multidimensional array.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int[,] array = new int[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; int length = array.GetLength(0); Console.WriteLine("Length is " + length); } } |
Output:
Length is 2
3. Using Array.Rank Property
If you need the number of dimensions of an array instead of the number of elements, use Array.Rank Property. It returns 1 for a one-dimensional array and jagged array (an array of arrays), 2 for a two-dimensional array, and so on.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
using System; public class Example { public static void Main() { int[,] array = new int[3, 5]; int rank = array.Rank; Console.WriteLine("Rank is " + rank); } } |
Output:
Rank is 2
4. Using foreach
Another possible, but less recommended, option is to manually count the number of elements in the array. The following example shows how to use a foreach loop and a counter variable to achieve the same.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
using System; public class Example { public static void Main() { int[] array = { 1, 2, 3, 4, 5 }; int length = 0; foreach (var item in array) { length++; } Console.WriteLine("Length is " + length); } } |
Output:
Length is 5
That’s all about finding the length of an array in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)