Use Array.Copy to copy values in two-dimensional arrays in C#


The Array.Copy method can copy values from one array to another. For one-dimensional arrays, this is reasonably straightforward. You specify the source and destination arrays, the indices where copying should start in the two arrays, and the number of items to copy. The process is the same for two-dimensional arrays, it’s just harder to visualize the result.

The items in a two-dimensional array are stored in a contiguous piece of memory in row-major order. That means the first row comes first, then the second row, then the third, and so forth. (In contrast, with column-major order, the first column would come first so the first sequence of values would include items from all of the rows.)

That means in memory the last item in a row is immediately followed by the first item in the following row.

When you use Array.Copy to copy two-dimensional arrays, the arrays are treated as if they were one-dimensional arrays with the values packed into it in row-major order. Instead of specifying the row and column in the array where the copying should start, you specify the index in the one-dimensional version. If you know the row and column where you want copying to start, it’s easy enough to calculate this index.

For example, if an array has num_cols columns and you want copying to start at row r and column c, then the “one-dimensional” index is:

index = r * num_cols + c

You can use this equation to calculate the index in the source array where copying should begin and to calculate the index in the destination array where the values should be copied. Remember that the two arrays may have different sizes, however, so the value num_cols may be different in the two arrays. (Notice that the number of rows doesn’t matter.)

When you call Array.Copy passing it these indices, if the values extends past the end of a row, they simply wrap around to the next row (because the values are packed in memory in row-major order).

This example uses the following code to copy values from one array to another.

// Copy the values and display the result.
private void btnCopy2d_Click(object sender, EventArgs e)
    string[,] new_values = new string[3, 3];
    for (int row = 0; row <= new_values.GetUpperBound(0); row++)
        for (int col = 0; col <= new_values.GetUpperBound(1); col++)
            new_values[row, col] = "------";

    Array.Copy(Values2d, 6, new_values, 2, 6);
    ShowValues(lstCopy, new_values);

The form's Load event handler, which isn't shown here, initializes the Values2d array. This code creates a new array called new_values with three rows and three columns. (Notice that this doesn't match the Values2d array, which has five rows and four columns.) The code then fills each entry in the new array with the string "------."

Next, the code calls Array.Copy to copy values from Values2d into new_values. It starts taking values from index 6 in the Values2d array. If you use the earlier equation or just count entries in the picture (starting at 0, as usual), you'll see that index 6 corresponds to row 1 column 2 where the entry is (1, 2).

The values are copied into new_values at index 2, which corresponds to row 0 column 2.

The code copies six items. Notice in the picture how the values from both arrays wrap from one row to another. (I added the red outlines to show which values are copied. The outlines are not part of the program's output.)

The Array.Copy method can copy values between higher-dimensional arrays but it is confusing. The next post will describe some extension methods that make it easier to copy between arrays in general and two-dimensional arrays in particular.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in arrays and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *