In one of the previous articles , we demonstrated the usage of the Length property of the array in C# to get the number of elements in it. In this post , lets have a look at getting the number of elements in the multi-dimentional array in C#.
How to get the Number of Elements in an MultiDimensional Array in C# ?
If you wanted to find out the total number of elements in the multi-dimensional array , you can use the GetLength method of the array and specify the dimension as parameter as shown in the code snippet.
using System;
namespace ACConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string[,] movieNames = {
{"Robo 2.0", "Vishwaroopam 2.0", "Theri 2.0"},
{"Mangatha", "Ghilli", "Billa"},
{"Kabali", "Sivakasi", "Aegan"},
{"Anegan", "Billa 2", "Sivaji"},
};
int rowsCount = movieNames.GetLength(0);
int ColumnCount = movieNames.GetLength(1);
Console.WriteLine("Total Number of Rows = " + rowsCount);
Console.WriteLine("Total Number of Columns = " + ColumnCount);
Console.WriteLine("Total Elements = " + rowsCount * ColumnCount);
Console.ReadLine();
}
}
}
Leave a Reply