C# Multidimensional Arrays

  Keywords : C# multi dimensional arrays can have more than one dimension with examples, C# two dimensional or three dimensional arrays with examples

Here we will learn multi-dimensional arrays in c# with examples and how to use c# multi-dimensional arrays to represent two or three dimensional series with examples.

C# Multi-Dimensional Arrays

As discussed in previous chapter, Arrays in C# will support multi-dimensional arrays.  In c#, a multidimensional array is an array which contains a more than one dimension to represent the elements in tabular format like rows and columns.

 

In c#, multidimensional arrays can support either two or three dimensional series. To create multidimensional arrays, we need to use comma (,) separator inside the square brackets.

C# Multi-Dimensional Array Declaration

In c#, Multidimensional Arrays can be declared by specifying the data type of an elements followed by the square brackets [] with comma (,) separator. Following are the examples of creating two or three dimensional arrays in c# programming language.

 

// Two Dimensional Array

int[,] arr = new int[4, 2];

// Three Dimensional Array

int[, ,] arr1 = new int[4, 2, 3];

If you observe above examples, we created a two dimensional array (arr) with 4 rows, 2 columns and we created another array (arr1) with three dimensions 4, 2, 3.

C# Multi-Dimensional Array Initialization

In c#, we can initialize an arrays upon declaration. Following are the different ways of declaring and initializing multidimensional arrays in c# programming language.

 

// Two Dimensional Integer Array

int[,] intarr = new int[3, 2] {

                                             { 4, 5 },

                                             { 5, 0 },

                                             { 3, 1 }

                                         };

// Two Dimensional Integer Array without Dimensions

int[,] intarr1 = new int[,] { { 4, 5 }, { 5, 0 }, { 3, 1 } };

// Three Dimensional Array

int[, ,] array3D = new int[2,2,3] { { { 1, 2, 3 }, { 4, 5, 6 } },

                                                    { { 7, 8, 9 }, { 10, 11, 12 } } };

// Three Dimensional Array without Dimensions

int[, ,] array3D1 = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },

                                                { { 7, 8, 9 }, { 10, 11, 12 } } };

If you observe above examples, we declared and initialized a two dimensional array with 3 rows and 2 columns and three dimensional array with 2, 2, 3 dimensions.

 

In second and fourth statements, while declaration we initialized an arrays with values, but without specifying any dimensions. Here, the dimensions of an array can be determined by the number of elements so the dimension initializers are not required if we are assigning an elements during the initialization.

 

Following diagram will illustrate more details about how multidimensional arrays will be divided into rows and columns in c# programming language.

 

C# Multidimensional Arrays Representation Diagram

 

If you observe above diagram, we defined a two dimensional array with 3 rows, 2 columns and inserting the elements in an array based on the defined number of elements.

C# Access Multidimensional Array Elements

In c#, we can access the values of a multidimensional arrays by using row index and column index values.

 

Following is the example of accessing an elements from multidimensional arrays in c# programming language based on our requirements.

 

// Two Dimensional Array

int[,] arr2D = new int[3, 2] {

                                            { 4, 5 },

                                            { 5, 0 },

                                            { 3, 1 }

                                         };

// Three Dimensional Array

int[, ,] array3D = new int[2,2,3] {

                                                   { { 1, 2, 3 }, { 4, 5, 6 } },

                                                   { { 7, 8, 9 }, { 10, 11, 12 } }

                                                };

Console.WriteLine(arr2D[1, 0]); // 5

Console.WriteLine(array3D[1,1,1]); // 11

If you observe above example, we are accessing an elements of two or three dimensional arrays by passing index values.

C# Multidimensional Array Example

Following is the example of using a multidimensional arrays in c# programming language to represent the elements in an array with multiple dimensions.

 

using System;

 

namespace Tutlane

{

    class Program

    {

        static void Main(string[] args)

        {

            // Two Dimensional Array

            int[,] array2D = new int[3, 2] { { 4, 5 }, { 5, 0 }, { 3, 1 } };

            // Three Dimensional Array

            int[, ,] array3D = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } };

            Console.WriteLine("---Two Dimensional Array Elements---");

            for (int i = 0; i < 3; i++)

            {

                for (int j = 0; j < 2; j++)

                {

                    Console.WriteLine("a[{0},{1}] = {2}", i, j, array2D[i, j]);

                }

            }

            Console.WriteLine("---Three Dimensional Array Elements---");

            for (int i = 0; i < 2; i++)

            {

                for (int j = 0; j < 2; j++)

                {

                    for (int k = 0; k < 3; k++)

                    {

                        Console.WriteLine("a[{0},{1},{2}] = {3}", i, j, k, array3D[i, j, k]);

                    }

                }

            }

            Console.WriteLine("Press Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we created a two and three dimensional arrays and getting those array values by using for loop in c#.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Multidimensional Arrays Example Result

 

If you observe above result, we are able to get the two or three dimensional arrays by using respective index values based on our requirements.

 

In c#, multidimensional arrays are slower than single dimensional arrays while accessing an elements from arrays. Instead of multidimensional arrays, we can use jagged arrays in c# to improve the performance of application.

 

This is how we can use multidimensional arrays in our c# applications based on our requirements.