By 22/07/2015 12 Comments

Multidimensional Arrays In C#

  • Multidimensional Arrays In C# are arrays that has two or more dimensions, and an individual element is accessed through the combination of two or more indices.
  • In a two dimensional array, the location of any specific element is specified by two indices.
  • It is also called 2D, 3D etc.
  • Suppose if there is a table of student’s roll no. information then :

1st index indicates the row and the 2nd index indicates the column.

General Syntax:

int [ , ] studentsRollNumbers = new int [2 , 3];

STATEMENT DESCRIPTION
[ , ] This syntax indicates that this is 2D (2 Dimensional Array).
[2 , 3] Here 2 are rows and 3 are columns then that rows multiply with the columns such as [2 x 3] = 6 means this table array can contains 6 elements.

Example – 1, Multidimensional Array (2D) using foreach loop:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {

      // An int type 2D Multidimensional Array declaration.
            int[,] studentsRollNumbers = new int[2, 3];

      // Statements assign each element in each individual multiple indices.
            studentsRollNumbers[0, 0] = 1;
            studentsRollNumbers[0, 1] = 2;
            studentsRollNumbers[0, 2] = 3;

            studentsRollNumbers[1, 0] = 4;
            studentsRollNumbers[1, 1] = 5;
            studentsRollNumbers[1, 2] = 6;

       // loop through each array’s row and column indices and print them.
            foreach (int displayInformation in studentsRollNumbers)
            {
                Console.WriteLine(displayInformation);
            }
            Console.ReadKey();
        }
        /*
        The Output will be:
        
        1
        2
        3
        4
        5
        6
       */
    }
}


NOTE: Multidimensional Arrays can also be declared and initialized as following:

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

Example – 2, Multidimensional Array using nested for loop:

using System;
namespace csharpBasic
{
    class Program
    {
        static void Main(string[] args)
        {
       
     // An int type Multidimensional (2D) array declaration and initialization.
            int[,] studentsRollNumbers = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };

    // An outer for, which loops through each rows of 2D array.
            for (int rows = 0; rows < 2; rows++)

    // An inner for, which loops through each columns of 2D Array.
                for (int cols = 0; cols < 3; cols++)
                    Console.WriteLine(studentsRollNumbers[rows, cols]);

            Console.ReadKey();
        }
        /*
        The Output will be:
        
        1
        2
        3
        4
        5
        6
       */
    }
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .