C# Flatten Array

Array elements

A multidimensional array can be flattened. Its dimensions are reduced to one. This transformation yields a single-dimensional array—one that is simpler to allocate and faster to use. Flattened 2D arrays are also ideal for interop with other languages.

This C# article shows how you can flatten 2D arrays into 1D arrays.

Flattened array index computation

array[(Y coordinate * width) + X coordinate]

2D array index computation

array[Y coordinate, X coordinate]

Flatten 2D arrays

First, in a 2D array, you access the cell by its Y position and then by its X position. The runtime does two bounds checks at this point. For rectangular arrays, which include all 2D arrays and many jagged arrays, you can use a single array and multiply the first coordinate by the width, and then add the second coordinate.

Advantages. The advantages of using a flat array are improved performance and interoperability with C++ or other languages. To use a jagged array, you must have an array of references to arrays. 2D arrays have significant performance penalties, sometimes of 54%.

Jagged Array Examples

Example

Here we see an example that creates, assigns to and finally displays a 2D array and its equivalent flattened array. It is contained in a console program with a simple CLI.

Program that uses flattened array [C#]

using System;

class Program
{
    static void Main()
    {
	while (true)
	{
	    Console.WriteLine("Enter height: [4+]");
	    int height = int.Parse(Console.ReadLine());

	    Console.WriteLine("Enter width: [10+]");
	    int width = int.Parse(Console.ReadLine());

	    //
	    // A. TWO DIMENSIONAL ARRAY
	    //
	    int[,] twoDimensional = new int[height, width];
	    // Assign cell 1, 6
	    twoDimensional[1, 6] = 5;
	    // Assign cell 3, 9
	    twoDimensional[3, 9] = 9;
	    // Assign cell at 2, 3
	    twoDimensional[2, 3] = 1;

	    // Display
	    for (int i = 0; i < height; i++)
	    {
		for (int a = 0; a < width; a++)
		{
		    Console.Write(twoDimensional[i, a]);
		}
		Console.WriteLine();
	    }
	    Console.WriteLine();

	    //
	    // B. FLATTENED ARRAY
	    //
	    int[] oneDimensional = new int[width * height];
	    // Assign cell 1, 6
	    oneDimensional[1 * width + 6] = 5;
	    // Assign cell 3, 9
	    oneDimensional[3 * width + 9] = 9;
	    // Assign cell at 2, 3
	    oneDimensional[2 * width + 3] = 1;

	    // Display
	    for (int i = 0; i < height; i++)
	    {
		for (int a = 0; a < width; a++)
		{
		    Console.Write(oneDimensional[i * width + a]);
		}
		Console.WriteLine();
	    }
	}
    }
}
Two-dimensional (2D)

Description. Part A shows the 2D array with traditional syntax; Part B shows the flattened 1D array. Notice how we use multiplication always when accessing the oneDimensional flat array. Here we see some output from the program that demonstrates how the multiplication results in correct output.

Possible output

Enter height: [4+]
4
Enter width: [10+]
10
0000000000
0000005000
0001000000
0000000009

0000000000
0000005000
0001000000
0000000009

Enter height: [4+]
5
Enter width: [10+]
15
000000000000000
000000500000000
000100000000000
000000000900000
000000000000000

000000000000000
000000500000000
000100000000000
000000000900000
000000000000000

Enter height: [4+]
6
Enter width: [10+]
12
000000000000
000000500000
000100000000
000000000900
000000000000
000000000000

000000000000
000000500000
000100000000
000000000900
000000000000
000000000000

Summary

The C# programming language

We saw how you can create, assign values to, and display flattened arrays. This is useful for performance, memory reduction, and interoperability with other systems. In every high-performance application I have applied this technique to, performance has improved. It is ideal for hashtable bucket implementations as well as certain trie structures.

Array Types
.NET