C# Multidimensional Array

Multidimensional array illustration

An array can have many dimensions. Multidimensional arrays are available using a special syntax in the C# language. They can be used to model complex spaces without using a lot of custom code.

This C# program uses a multidimensional array that has three dimensions.

Example

Note

First, this example program written in the C# language demonstrates the use of a three-dimensional array. You can declare and initialize the multidimensional array using the comma syntax. Also, you can loop over any three-dimensional array using this example code, and add levels to the loop to handle greater dimensions in the array.

Program that uses three-dimensional array [C#]

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();
	}
    }
}

Output

100
200
300
400
500

000
020
000
000
000

000
000
003
000
000

000
000
004
000
000
Main method

Explanation. This example program text is contained in a compilation unit and it has a Main entry point. The program text uses a int[, ,] type, which is a three-dimensional integer array reference type. When using a multidimensional array, you should specify the dimension lengths on the array creation expression, not the type itself.

Comma syntax. You can access elements in a two-dimensional or three-dimensional array using the comma syntax in the array index. You can read and write elements using this syntax. The program first shows how you can write elements, and then shows how you can read elements in a nested loop.

2D Array Examples

Using GetLength method with parameter. The GetLength instance method is available on all constructed array types. To use the GetLength method, pass it one parameter that is the dimensions of the array you want to get the length of. A three-dimensional array has three allowed values, and you can access the dimension 0, dimension 1, and dimension 2.

Uses

Note

Let's consider the uses of multidimensional arrays such as three-dimensional arrays in the C# language. In computer science textbooks, multidimensional arrays are used to model problems that occur in the real world in several dimensions.

For example, two dimensional arrays model a plane while three-dimensional arrays model a cube or other structure. Structures such as four-dimensional arrays can be used for systems that do not truly require four dimensions but just need more space for a certain algorithm at that level.

Unfortunately, in the C# language, two-dimensional arrays and all multi-dimensional arrays are slower than one-dimensional arrays to access elements in. Instead, jagged arrays can be used.

Jagged Array Examples

Credit: Here at Dot Net Perls world headquarters, I occasionally make mistakes on code examples. The original version of this article printed out the 3D array in an incorrect way. Fortunately, Kenny Lindstrom wrote in with a correction.

Summary

The C# programming language

We looked at multidimensional arrays in the C# programming language and a three-dimensional array in particular. The comma declarator syntax is used to specify this exact type and you can loop over the arrays in order using some helper methods on the array type. Finally, we noted some actual uses of multidimensional arrays in languages and also reviewed some optional implementations.

Array Types
.NET