How to get the Number of Elements in an MultiDimensional Array in C# ?


This content was proof-read by Grammarly. You can Try Grammary for FREE

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.


Are you a .NET Developer planning to integrate GraphQL for .NET in your Application? Check out this book
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();
        }
    }
}
image
%d bloggers like this: