SoFunction
Updated on 2025-03-06

Definition and element access of rectangular arrays in C#

A rectangular array refers to a two-dimensional array composed of elements of the same data type according to rows and columns. You can use an index to access a single element in a rectangular array, or you can use a loop structure to traverse all elements in a rectangular array. In addition, we need to be careful not to modify the dimensions of the rectangular array to avoid problems such as excessive memory usage caused by using rectangular arrays.

Define a rectangle array

Before we start discussing how to access elements in a rectangular array, let's first introduce the concept of a rectangular array. A rectangular array refers to a two-dimensional array composed of elements of the same data type according to rows and columns.

In C#, you can use the following syntax to define a rectangular array:

int[,] matrix = new int[3, 4];

This defines a rectangular array with 3 rows and 4 columns, where each element has an initial value of 0.

How to access elements in a rectangular array?

Access elements using indexes

In C#, you can use indexes to access elements in a rectangular array. The index has two parameters, the first parameter represents the row number and the second parameter represents the column number.

For example, if we want to access the element in row 2, column 3 in a rectangular array, we can use the following code:

int element = matrix[1, 2];

Where, the value of the element will be stored in the variable element.

Iterate through all elements in a rectangular array

In addition to accessing a single element through an index, we can also use a loop structure to iterate through all elements in a rectangular array.

The following code demonstrates how to traverse a rectangular array using a double loop:

for (int row = 0; row < (0); row++)
{
    for (int col = 0; col < (1); col++)
    {
        int element = matrix[row, col];
        (element + " ");
    }
    ();
}

In the above code, we use the GetLength() method to get the number of rows and columns of the rectangular array, and iterate through all elements in the array in sequence in a double loop. In each inner loop, we use the variable element to store the currently accessed element and print it to the console.

Modify the element values ​​in the rectangle array

In C#, we can use indexes to modify element values ​​in a rectangular array. For example, the following code changes the element value of row 2, column 3 in the rectangular array to 10:

matrix[1, 2] = 10;

Notes on rectangular arrays

Cannot change the dimensions of rectangular arrays

In C#, we cannot modify the dimensions of rectangular arrays. For example, the following code will cause a compilation error:

(0) = 4;

This is because the GetLength() method returns the length of the array, not a modifiable value.

The data types of rectangular arrays must be consistent

The data types of all elements in the rectangular array must be consistent. If the array contains elements of different types, it will cause a compilation error.

Try to avoid using rectangular arrays

Although rectangular arrays are very convenient in some cases, in actual development, try to avoid using rectangular arrays. Because the use of rectangular arrays will cause excessive memory usage and it is difficult to dynamically scale.

Summarize

Through the introduction of this article, we learned the definition and element access of rectangular arrays in C#. We learned that a rectangular array is a two-dimensional array of elements of the same type, consisting of rows and columns, and can use an index to access a single element in a rectangular array, or iterate through all elements in a rectangular array using a loop structure. In addition, we need to be careful not to modify the dimensions of the rectangular array to avoid problems such as excessive memory usage caused by using rectangular arrays.

This is the article about the definition of rectangular arrays and element access in C#. For more related rectangular array content in C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!