SoFunction
Updated on 2025-03-06

Detailed explanation of the use of one-dimensional arrays and multi-dimensional arrays in C# programming

One-dimensional array
A one-dimensional array of five integers can be declared as shown in the following example.

int[] array = new int[5];

This array contains elements from array[0] to array[4]. The new operator is used to create arrays and initialize array elements to their default values. In this example, all array elements are initialized to zero.
An array that stores string elements can be declared in the same way. For example:

string[] stringArray = new string[6];

Array initialization
It can be initialized when an array is declared, in which case there is no need for a level specifier because the level specifier is already provided by the number of elements in the initialization list. For example:

int[] array1 = new int[] { 1, 3, 5, 7, 9 };

The string array can be initialized in the same way. The following declares an array of strings, where each array element is initialized with the name of the day:

string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

If you initialize the array when declaring it, you can use the following shortcut:

int[] array2 = { 1, 3, 5, 7, 9 };
string[] weekDays2 = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

An array variable can be declared but not initialized, but the new operator must be used when assigning an array to this variable. For example:

int[] array3;
array3 = new int[] { 1, 3, 5, 7, 9 }; // OK
//array3 = {1, 3, 5, 7, 9}; // Error

Value type array and reference type array
Please see the following array declaration:

SomeType[] array4 = new SomeType[10];

The result of this statement depends on whether SomeType is a value type or a reference type. If it is a value type, the statement creates an array of 10 elements, each with the SomeType type. If SomeType is a reference type, the statement creates an array of 10 elements, each of which is initialized as an empty reference.


Multidimensional array
An array can have multiple dimensions. For example, the following declaration creates a two-dimensional array with four rows and two columns.

int[,] array = new int[4, 2];

The following declaration creates a three-dimensional (4, 2, and 3) array.

int[, ,] array1 = new int[4, 2, 3];

Array initialization
You can initialize the array when declaring it, as shown in the following example.

// Two-dimensional array.
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// A similar array with string elements.
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
          { "five", "six" } };

// Three-dimensional array.
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
         { { 7, 8, 9 }, { 10, 11, 12 } } };
// The same array with dimensions specified.
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } }, 
          { { 7, 8, 9 }, { 10, 11, 12 } } };

// Accessing array elements.
(array2D[0, 0]);
(array2D[0, 1]);
(array2D[1, 0]);
(array2D[1, 1]);
(array2D[3, 0]);
(array2Db[1, 0]);
(array3Da[1, 0, 1]);
(array3D[1, 1, 2]);

// Getting the total count of elements or the length of a given dimension.
var allLength = ;
var total = 1;
for (int i = 0; i < ; i++) {
 total *= (i);
}
("{0} equals {1}", allLength, total);

Output:

1
2
3
4
7
three
8
12
12 equals 12

The array can also be initialized without specifying a level.

int[,] array4 = { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

If you choose to declare an array variable but do not initialize it, an array must be assigned to this variable using the new operator. The following example shows how new is used.
int[,] array5;
array5 = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // OK
//array5 = {{1,2}, {3,4}, {5,6}, {7,8}}; // Error

The following example assigns values ​​to specific array elements.

array5[2, 1] = 25;

Likewise, the following example takes the value of a specific array element and assigns it to the variable elementValue.

int elementValue = array5[2, 1];

The following code example initializes array elements to default values ​​(except interleaved arrays):

int[,] array6 = new int[10, 10];