What is a 4D array
A four-dimensional (4D) array is an array of arrays. In other words, a 4D array is a multidimensional array with four dimensions. It can be used to represent data that requires four indices to access. To declare a 4D array in C#, you need to specify the size of each dimension.
For example, the following code declares a 4D array with dimensions of 2, 3, 4, and 5 C# allows for multidimensional arrays with up to 32 dimensions. For instance, [,] declares a two-dimensional array, [,] a three-dimensional array, [,] a four-dimensional array, and so on in.
int[,,,] myArray = new int[2, 3, 4, 5];
This creates a 4D array with two elements in the first dimension, three elements in the second dimension, four elements in the third dimension, and five elements in the fourth dimension. Each element in the array is initialized to the default value for the element type, which is 0 for integers. You can access the elements of a 4D array using four indices like this.
myArray[0, 1, 2, 3] = 42;
This sets the element at position (0, 1, 2, 3) to the value 42. You can also use nested loops to iterate over the elements of a 4D array, like this
public class Program
{
public static void Main()
{
int[,,,] Array4d = new int[1, 1, 2, 2]{
{
{{1, 2}, {3, 4}}
}
};
int[,,,] Array41 = new int[1, 2, 2, 2]{
{
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
}
};
Console.WriteLine("Array4d");
Console.WriteLine(Array4d[0, 0, 0, 0]);
Console.WriteLine(Array4d[0, 0, 0, 1]);
Console.WriteLine(Array4d[0, 0, 1, 0]);
Console.WriteLine(Array4d[0, 0, 1, 1]);
Console.WriteLine("Array41");
Console.WriteLine(Array41[0, 0, 0, 0]);
Console.WriteLine(Array41[0, 0, 0, 1]);
Console.WriteLine(Array41[0, 0, 1, 0]);
Console.WriteLine(Array41[0, 0, 1, 1]);
Console.WriteLine(Array41[0, 1, 0, 0]);
Console.WriteLine(Array41[0, 1, 0, 1]);
Console.WriteLine(Array41[0, 1, 1, 0]);
Console.WriteLine(Array41[0, 1, 1, 1]);
}
}
This program declares and initializes two multidimensional arrays, Array4d and Array41. Array4d is a 4-dimensional array with dimensions of 1x1x2x2, meaning it contains a single 2x2 matrix. Array41 is a 4-dimensional array with dimensions of 1x2x2x2, meaning it contains two 2x2 matrices.
The program then prints out the values of each element in both arrays using the indexing notation array[x, y, z, w]. The values printed out correspond to the elements of the matrices in each array.
Here is the output of the program
new int[1, 2, 2, 2]{
{
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
}
};
This initializes the only element of the array with a 2x2 matrix of integers, where the first row contains the values 1 and 2 and the second row contains the values 3 and 4.
The second array Array41 is declared as a 4-dimensional array with a length of 1 in the first dimension and a length of 2 in each of the remaining dimensions. The values of this array are initialized with the following expression.
new int[1, 2, 2, 2]{
{
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
}
};
This initializes the two elements of the array with 2x2 matrices of integers, where the first element contains the values 1, 2, 3, and 4, and the second element contains the values 5, 6, 7, and 8.
After initializing the arrays, the program prints out some values using the indexing operator []. The output of the program is as follows.
Output
This output corresponds to the values of the arrays accessed using the indexing operator in row-major order.
Conclusion
In this article, you will learn about code that taught us What a 4D array in C# is. 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)
FAQs
Can you give an example of a real-world scenario where a 4D array would be useful?
A real-world scenario where a 4D array could be useful is in medical imaging, specifically in magnetic resonance imaging (MRI) scans. MRI machines capture images of the human body in three dimensions (3D), but they also capture multiple images over time to show how a specific area is changing. These time series images can be stored in a 4D array, with the first three dimensions representing the x, y, and z coordinates of the image and the fourth dimension representing time. This allows medical professionals to track changes over time and make more accurate diagnoses.