c sharp

C# Multidimensional Array

As we all know, arrays are the data structure having the ability to hold more than one same type of value simultaneously using the different index locations. We don’t need to create more than one variable time and again. We only need to declare a simple array variable and add values to it. Arrays are also said to be “Matrix,” having different dimensions, i.e., multidimensional arrays. Thus, we have been using this article to cover the maximum multi-dimensional arrays in C# programming while using the shell of the Ubuntu 20.04 platform. Let’s make a start for this article by making use of the “touch” instruction to create a C# file.

Example 01: 1-dimensional Array

We will be starting the concept of the array with the 1-dimensional array. The 1-dimensional array contains a single row and a single column. Starting from the main structure of the C# program, we have added the “System” library at the first line of code for using the related input and output functions in the codes.

After this, we have created a new namespace and named it ‘arrays’. We have created a user-defined class “array” and its main() driver function within this namespace. We have initialized an integer array “Arr” of size 10 with 10 integer values in it. For this, we have to use the single “curly” brackets. To display the value of a 1-dimensional array on a separate line, we have been using the ‘for’ loop here for up to 10 iterations. This loop will continue to execute the “Console.WriteLine()” function statement to display each value of the array using its index “i”.

After the code is complete, save it. Try Compiling it with the “mcs” C# compiler in Ubuntu. The “exe” file will be generated in the current home location. We will be using that “exe” file to run the compiled code via the “mono” runtime executor of C# in Linux. The output shown below displays each value of the array at different indexes separately on each line.

Example 02: 2-dimensional Array

Of many multi-dimensional arrays, the most basic one is a 2-dimensional array. This type of array may never contain the same number of rows and columns, and it must have 2 dimensions. Let’s use this example to elaborate on the syntax and use of 2-dimensional arrays. We have been using the very same C# code structure, i.e., System library, “arrays” namespace, “array” class, and the “main” function.

Within this main() method, we have been initializing a 2-dimensional array named “Arr” with “,” in square brackets showing the 2-dimensional syntax for arrays. Within the initialization line, we have been defining the size for this array as 3 rows and 2 columns. We have been using two ‘for’ loops here to iterate the rows and columns. The “I” iterator loop is for “rows” and “j” for the iteration of “columns” in an array “Arr”.

The iterations will be performed according to array sequence, i.e., row*column (1st row: 1st column, 1st row: 2nd column, 2nd row: 1st column, 2nd row 2nd column, 3rd row: 1st column, and 3rd row: 2nd column). Let’s display this array with the “Write()” function of the Console class.

After running this code “exe” file, we have got all the values at each location of a 2-dimensional array displayed on the shell at a single line. Each row and column is mentioned for each displayed array value on the shell screen.

If you want to avoid displaying the values of a 2-dimensional array on a single line, try using the “Console.WriteLine()” function statement instead of the “Console.Write” function statement. Let’s update our code below to see the changes.

We have compiled the updated code and executed its “exe” file with the “mono” runtime. The values of the 2-dimensional array are displayed on the separate lines of the shell.

Example 03: 3-dimensional Array

Let’s move towards the 3-dimensional array declaration and its usage in the C# code. So, start your updated code with the same general “System” library, arrays namespace, ‘array’ class, and public static main() function. We have been initializing an integer type 3-dimensional array “Arr” with double “,” in the square brackets. While initializing, we have to use the ‘new’ keyword and the type of an array and the size of an array in the square brackets. We are initializing an array of 3 rows, 3 columns, and an inner 1-column value.

You can see that the initialization of this array started with 3 curly brackets and ended at 3 curly brackets with at least 1 value in each row and column. To iterate this 3-dimensional array, we have to utilize three “for” loops. Outer two loops will be for “row” and “columns”, while the inner loop “k” will be for the inner “column-row” value iteration. The loop iteration size must be equal to the size of rows and columns defined in the array declaration at the start.

The loops will continue to display each value at each index of an array using the Console on each iteration.Writeline() function here. We have been using the Console.WriteLine() function again to give a line break and display the array in a 3-dimensional format on the shell.

After this code compilation and execution of its “exe” file, we have got the array displayed in a 3-dimensional format on our Ubuntu shell screen. It has 3 rows, 3 columns, and 1 value.

You can also assign the value to a particular location of an array without initializing it directly. You need to declare an array ‘Arr’ first with 3 rows, 3 columns, and 3 indexes. We have assigned values to each index of an array separately by the assignment method and used the same three “for” loops to iterate and display the 3-dimensional array values with “WriteLine.”

After this code execution, we have got separate 3 values for each of 3 rows and 3 columns values for the “Arr” array.

Example 04: 4-dimensional Array

Let’s end this tutorial guide with an explanation of declaring and initializing the 4-dimensional array “Arr” in the main() method. We have declared and initialized 4 rows, 4 columns, and inner 1 row, 2 columns for this particular array. You can see that this array has 2 inner columns for each inner row. The 4 “for” loops will be used to iterate this array. Outer 2 loops will be for outer rows and columns, while the inner two loops will be for inner 1 row and 2 columns. The iterator indexed has been used to display the array.

After execution, we have got 4 rows, 4 column array of 4-dimensions.

Conclusion

Henceforth, multi-dimensional arrays are simple and easy to use in any programming platform like C#, as discussed in this tutorial. We have started our illustrations from the most-basic arrays, i.e., 1-dimensional arrays. After this, we have covered most types of arrays like 2-dimensional arrays, 3-dimensional arrays, and 4-dimensional arrays. We have used different methods to declare and initialize the multi-dimensional arrays In C# codes. Also, we have tried to use different rows into column values for declaring the multi-dimensional arrays to get different style arrays.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.