In the previous post, we have seen how to print single-dimensional arrays in C#. This post will discuss how to print multidimensional arrays in C#.

1. Using foreach loop

The foreach statement provides a simple, clean way to iterate through the elements of a multidimensional array. The following example shows the usage of the foreach statement for printing multidimensional arrays in C#.

Download  Run Code

 
However, with multidimensional arrays, using a nested for-loop gives you more control over the order in which to process the array elements.

Download  Run Code

2. Using String.Join() method

Another good alternative is to use the String.Join() method, which concatenates the elements of the specified array using the specified separator.

Download  Run Code

3. Using List.ForEach() method

Another approach is to use the ToList() method to convert the multidimensional array into a generic List<T> and call List’s ForEach() method to print each element of the list. Note that this approach includes creating the list as an intermediate step, which is not recommended.

For multidimensional arrays, we can cast the source sequence elements to the specified type, as shown below:

Download  Run Code

That’s all about printing a multi-dimensional array in C#.