Introduction
This article will teach us about three-dimensional (3D) arrays in the C#.
What is a 3D Array in C#?
Arrays can be categorized based on their size. 3D arrays are used to hold multiple 2D arrays, each holding data of the same type. Multiple dimensions arrays with more than one dimension are possible in C#. Multidimensional arrays may support approximately 32 dimensions in C#. Commas should be included between the square brackets to indicate the dimensions of a multidimensional array.
You may determine an array's dimension by counting the commas. In a multidimensional array, there are fewer commas than there are dimensions. As an illustration, [,] is a 2D array, [,,] a 3D array, [,,,] a 4D array in C#.
The following example is used to access elements from Three-Dimensional arrays in the C#.
//Three-Dimensional Array
int[,,] arr3D = new int[2, 2, 3] {
{ { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } }
};
Console.WriteLine(arr3D[2, 2, 2]); // 22
}
}
}
The code declares and initializes a three-dimensional integer array named "arr3D". The array has dimensions of 2 x 2 x 3, meaning it has 2 layers, 2 rows, and 3 columns in each row.
It is initialized with the values {1, 2, 3} in the first row of the first layer, {4, 5, 6} in the second row of the first layer, {7, 8, 9} in the first row of the second layer, and {10, 11, 12} in the second row of the second layer.
The statement Console.WriteLine(arr3D[2, 2, 2]); attempts to access an element at index (2, 2, 2) of the array, but this is out of bounds. The array has indexes ranging from 0 to its length minus 1 in each dimension, so the last valid index is (1, 1, 2).
Therefore, the code will throw an "IndexOutOfRangeException" exception when executed.
If you observe the above examples, you declared and initialized a three-dimensional array with 2, 2, and 3 dimensions. In the second and fourth statements, while declarations, we initialized arrays with values 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 assign elements during the initialization in C#.
internal class Class1 {
static void Main(string[] args) {
// Three Dimensional Array
int[,,] arr3D = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } } };
Console.WriteLine("---Three Dimensional Array Elements---");
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 3; c++) {
Console.WriteLine("a[{0},{1},{2}] = {3}", a, b, c, arr3D[a, b, c]);
}
}
}
Console.WriteLine("Press Enter Key to Exit..");
Console.ReadLine();
}
}
}
This is a program that demonstrates the use of a three-dimensional array. The program creates a three-dimensional integer array arr3D with dimensions 2x2x3 and initializes its elements with values 1 to 12. It then uses three nested loops to iterate over all the elements of the array and print their values along with their indices. Finally, the program waits for the user to press the Enter key before exiting.
The output of the program will be,
Output
Conclusion
This code example taught us What a 3d Array is in C#. To learn everything about arrays in C#, check out Become a Master of Working With C# Arrays - A Comprehensive Guide to Arrays (c-sharpcorner.com)