By 22/07/2015 7 Comments

Three Dimensional Array In C#

  • Three dimensional array in C# can be declared easily.
  • Some time it might be used in programs.
  • It is also called 3D arary.
  • 3D Array stores each single element in its three indices locations and that can be accessed by these three indices.

General Syntax:

type [ , , ] array-name = new type[size1 * size2 * size3];

STATEMENTS DESCRIPTION
[ , , ] A two commas (,,) inside square bracket indicates that it is a Three Dimensional Array 3D.
[size1 * size2 *size3] Size bracket indicates the size of an array, here the size will be given three times and it will be multiply in each other e.g: [2 * 2 *2] = 8.

Example:

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

        // An int type three dimensional array 3D decalration.
            int[, ,] studentsRollNumbers = new int[2, 2, 2];

        // Statements are used to assign each elements in each individual 3D array’s indices.
            studentsRollNumbers[0, 0, 0] = 1;
            studentsRollNumbers[0, 0, 1] = 2;
            studentsRollNumbers[0, 1, 0] = 3;
            studentsRollNumbers[0, 1, 1] = 4;


            studentsRollNumbers[1, 0, 0] = 5;
            studentsRollNumbers[1, 0, 1] = 6;
            studentsRollNumbers[1, 1, 0] = 7;
            studentsRollNumbers[1, 1, 1] = 8;

         //First loop iterates the one dimensional indices.
         //Second loop iterates the two dimensional indices.
         //Third loop iterates the third dimensional indices.

            for (int OneD = 0; OneD < 2; OneD++)
                for (int TwoD = 0; TwoD < 2; TwoD++)
                    for (int ThreeD = 0; ThreeD < 2; ThreeD++)

                        Console.WriteLine(studentsRollNumbers[OneD, TwoD, ThreeD]);

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





Next 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 .