Multidimensional ArrayUse an array that has three dimensions. See the syntax for multidimensional arrays.
C#
Multidimensional array. An array can have many dimensions. Multidimensional arrays are available using a special syntax in the C# language.
Usage info. Multidimensional arrays model complex spaces without using a lot of custom code. In rare programs they are useful, but often are more complex than needed.
2D Array
Example program. Here we see the use of a three-dimensional array. You can declare and initialize the multidimensional array using the comma syntax.
Detail You can loop over any 3D array using this example code. Adding levels to the for-loop allows it to handle greater dimensions.
Here We use a three-dimensional integer array reference type. We specify the dimension lengths on the array creation expression.
Int Array
Array
Also We access elements in a 2D or 3D array using the comma syntax in the array index. We read and write elements using this syntax.
using System; class Program { static void Main() { // Create a three-dimensional array. int[, ,] threeDimensional = new int[3, 5, 4]; threeDimensional[0, 0, 0] = 1; threeDimensional[0, 1, 0] = 2; threeDimensional[0, 2, 0] = 3; threeDimensional[0, 3, 0] = 4; threeDimensional[0, 4, 0] = 5; threeDimensional[1, 1, 1] = 2; threeDimensional[2, 2, 2] = 3; threeDimensional[2, 2, 3] = 4; // Loop over each dimension's length. for (int i = 0; i < threeDimensional.GetLength(2); i++) { for (int y = 0; y < threeDimensional.GetLength(1); y++) { for (int x = 0; x < threeDimensional.GetLength(0); x++) { Console.Write(threeDimensional[x, y, i]); } Console.WriteLine(); } Console.WriteLine(); } } }
100 200 300 400 500 000 020 000 000 000 000 000 003 000 000 000 000 004 000 000
GetLength. This method is available on all constructed array types. To use the GetLength method, pass it one parameter. This parameter is the dimension of the array you want to check.
Tip A 3D array has three allowed values. You can access the dimension 0, dimension 1 and dimension 2.
Discussion. Let's consider the uses of multidimensional arrays. In computer science textbooks, multidimensional arrays model problems that occur in the real world in several dimensions.
Detail 2D arrays model a plane while three-dimensional arrays model a cube or other structure.
And Structures such as 4D arrays can be used for systems that do not truly require four dimensions but just need more space.
Warning In the C# language, multi-dimensional arrays are slower than 1D arrays when accessing elements. Instead, jagged arrays can be used.
Jagged Arrays
Summary. We looked at multidimensional arrays in the C# language. With some helper methods on the array type, you can loop over the arrays in order.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.