Advertisement
type[,,] array-name;
//C# Example of a three-dimensional array
using System;
class A
{
public static void Main()
{
//Declaration of a 3D array, which will hold arrays of int values.
int[,,] threeDA1;
//Declaring a 3D array holding arrays of object references of the type class A.
A[,,] threeDA2;
}
}
type[,,] array-name = new type[first-dimension , second-dimension, third-dimension]
where:
//C# Example of constructing a three-dimensional array
using System;
class A
{
public static void Main()
{
//A 3D Array is constructed on the heap with a size to hold 3 2D int arrays.
//Where each array has 2 rows and 2 columns in it.
int[,,] twoDA1= new int[3,2,2];
}
}
Array type | Default value |
---|---|
char | '\u0000' |
byte | 0 |
short | 0 |
int | 0 |
long | 0 |
float | 0 |
double | 0 |
bool | false |
String | null |
Object | null |
Advertisement
//C# Example of initialization of a three-dimensional array
using System;
class A
{
public static void Main()
{
//A 3D array holding three 2D int arrays(each array holding 2 rows and 2 columns of int values).
int[,,] threeDA= new int[3,2,2];
threeDA[0,0,0]=10; //inserting 10 in the first(0th index) array at its 0th row and 0th column index
threeDA[0,0,1]=15; //inserting 15 in the first(0th index) array at its 0th row and 1st column index
threeDA[0,1,0]=20; //inserting 20 in the first(0th index) array at its 1st row and 0th column index
threeDA[0,1,1]=25; //inserting 25 in the first(0th index) array at its 1st row and 1st column index
threeDA[1,0,0]=30; //inserting 30 in the second(1st index) array at its 0th row and 0th column index
threeDA[1,0,1]=35; //inserting 35 in the second(1st index) array at its 0th row and 1st column index
threeDA[1,1,0]=40; //inserting 40 in the second(1st index) array at its 1st row and 0th column index
threeDA[1,1,1]=45; //inserting 45 in the second1stindex) array at its 1st row and 1st column index
threeDA[2,0,0]=50; //inserting 50 in the third(2nd index) array at its 0th row and 0th column index
threeDA[2,0,1]=55; //inserting 55 in the third(2nd index) array at its 0th row and 1st column index
threeDA[2,1,0]=60; //inserting 60 in the third(2nd index) array at its 1st row and 0th column index
threeDA[2,1,1]=65; //inserting 65 in the third(2nd index) array at its 1st row and 1st column index
}
}
twoDA[0,0,0]=10;
The index position of the first 2D array in our 3D array is specified by the first-dimension i.e. zero, followed by a comma, followed by the
index position the row in the first array specified by the second-dimension i.e. zero,
followed by a comma, followed by the
index position the column in the first array specified by the third-dimension i.e. zero, at which we are inserting an int value 10.threeDA[0,0,1]=15
We are inserting 15 in the first(0th index) array at its 0th row and 1st column index.threeDA[0,1,0]=20
We are inserting 20 in the first(0th index) array at its 1st row and 0th column index .threeDA[0,1,1]=25
We are inserting 20 in the first(0th index) array at its 1st row and 1st column index .
And like this, we have inserted the elements in the next two 2D arrays as well.Console.WriteLine(twoDA[0,0,0]); //Reading the first(1st index) array and its zeroth(0th index) element
//C# Example of reading a 3D array holding 2D arrays of primitive values.
using System;
class A
{
public static void Main()
{
//A 3D array holding three 2D int arrays(each array holding 2 rows and 2 columns of int values).
int[,,] threeDA= new int[3,2,2];
threeDA[0,0,0]=10; //inserting 10 in the first(0th index) array at its 0th row and 0th column index
threeDA[0,0,1]=15; //inserting 15 in the first(0th index) array at its 0th row and 1st column index
threeDA[0,1,0]=20; //inserting 20 in the first(0th index) array at its 1st row and 0th column index
threeDA[0,1,1]=25; //inserting 25 in the first(0th index) array at its 1st row and 1st column index
threeDA[1,0,0]=30; //inserting 30 in the second(1st index) array at its 0th row and 0th column index
threeDA[1,0,1]=35; //inserting 35 in the second(1st index) array at its 0th row and 1st column index
threeDA[1,1,0]=40; //inserting 40 in the second(1st index) array at its 1st row and 0th column index
threeDA[1,1,1]=45; //inserting 45 in the second1stindex) array at its 1st row and 1st column index
threeDA[2,0,0]=50; //inserting 50 in the third(2nd index) array at its 0th row and 0th column index
threeDA[2,0,1]=55; //inserting 55 in the third(2nd index) array at its 0th row and 1st column index
threeDA[2,1,0]=60; //inserting 60 in the third(2nd index) array at its 1st row and 0th column index
threeDA[2,1,1]=65; //inserting 65 in the third(2nd index) array at its 1st row and 1st column index
//Checking the total numbers of elements in our 3D array
//By using the Length property of array
Console.WriteLine("Length of our 3D array is : " + threeDA.Length);
Console.WriteLine("Contents of this array are : ");
//Reading our 3D array by using nested for-loops
for(int array=0; array<3; array++)
{
for(int row=0; row<2; row++)
{
for(int column=0; column<2; column++)
{
Console.WriteLine(threeDA[array,row, column]);
}
}
}
}
}
Length of our 3D array is : 12
Contents of this array are :
10
15
20
25
30
35
40
45
50
55
60
65
Advertisement
//C# Example of reading a 3D array holding 2D arrays of object references
using System;
class A
{
public static void Main()
{
//A 3D array holding three 2D arrays(each array holding 2 rows and 2 columns of object references of class A).
A[,,] threeDA= new A[3,2,2];
threeDA[0,0,0] = new A(); //inserting an object of A in the first(0th index) array at its 0th row and 0th column index
threeDA[0,0,1] = new A(); //inserting an object of A in the first(0th index) array at its 0th row and 1st column index
threeDA[0,1,0] = new A(); //inserting an object of A in the first(0th index) array at its 1st row and 0th column index
threeDA[0,1,1] = new A(); //inserting an object of A in the first(0th index) array at its 1st row and 1st column index
threeDA[1,0,0] = new A(); //inserting an object of A in the second(1st index) array at its 0th row and 0th column index
threeDA[1,0,1] = new A(); //inserting an object of A in the second(1st index) array at its 0th row and 1st column index
threeDA[1,1,0] = new A(); //inserting an object of A in the second(1st index) array at its 1st row and 0th column index
threeDA[1,1,1] = new A(); //inserting an object of A in the second1stindex) array at its 1st row and 1st column index
threeDA[2,0,0] = new A(); //inserting an object of A in the third(2nd index) array at its 0th row and 0th column index
threeDA[2,0,1] = new A(); //inserting an object of A in the third(2nd index) array at its 0th row and 1st column index
threeDA[2,1,0] = new A(); //inserting an object of A in the third(2nd index) array at its 1st row and 0th column index
threeDA[2,1,1] = new A(); //inserting an object of A in the third(2nd index) array at its 1st row and 1st column index
//Checking the total numbers of elements in our 3D array
//By using the Length property of array
Console.WriteLine("Length of our 3D array is : " + threeDA.Length);
Console.WriteLine("Contents of this array are : ");
//Reading our 3D array by using nested for-loops
for(int array=0; array<3; array++)
{
for(int row=0; row&;t;2; row++)
{
for(int column=0; column<2; column++)
{
Console.WriteLine(threeDA[array,row, column]);
}
}
}
}
}
Length of our 3D array is : 12
Contents of this array are :
A
A
A
A
A
A
A
A
A
A
A
A
//C# A 3D array holding object references will be automatically initialized to null
using System;
class A
{
public static void Main()
{
//A 3D array holding three 2D arrays(each array holding 2 rows and 2 columns of object referenced of class A).
A[,,] threeDA= new A[3,2,2];
//Checking the total numbers of elements in our 3D array
//By using the Length property of array
Console.WriteLine("Length of our 3D array is : " + threeDA.Length);
Console.WriteLine("Contents of this array are : ");
//Reading our 3D array by using nested for-loops
for(int array=0; array<3 array++)
{
for(int row=0; row<2; row++)
{
for(int column=0; column<2; column++)
{
if( threeDA[array,row,column]== null)
Console.WriteLine(threeDA[array,row, column] + "A null element");
}
}
}
}
}
Length of our 3D array is : 12
Contents of this array are :
A null element
A null element
A null element
A null element
A null element
A null element
A null element
A null element
A null element
A null element
A null element
A null element
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement