Print 2D Array (Matrix) In C#

What is a multidimensional or 2D array in C#?

The multidimensional array contains more than one row to store the data, hence its index will be two numbers in a pair, one identifying the row and one for the column. This type of array contains the same length of elements in each row, hence, it is also known as a rectangular array.
 
The following program illustrates the multidimensional array where we are creating an array by accepting row and column size from a user, and by accepting input from the user and displaying the final array.
 
Note
In this program the user fills the array by providing value in one line with empty space. 
 

The Program for Printing Matrix or 2D Array in C#

  1. using System;  
  2.   
  3. namespace ConsoleMultipleClass  
  4. {  
  5.     class _2DArray  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             _2DArray obj = new _2DArray();  
  10.             obj.TwoDArray();  
  11.             Console.ReadLine();  
  12.         }  
  13.   
  14.         void TwoDArray()  
  15.         {  
  16.             Console.Write("Define Column Number:- ");  
  17.             int column = Convert.ToInt32(Console.ReadLine());  
  18.   
  19.             Console.Write("Define Row Number:- ");  
  20.             int rowNO = Convert.ToInt32(Console.ReadLine());  
  21.   
  22.             Console.WriteLine("----------------------------------------------");  
  23.   
  24.             Console.Write("Now Fill Array:- ");  
  25.   
  26.             int[,] TwoDArray = new int[column, rowNO];  
  27.   
  28.             string arrayTemp = Convert.ToString(Console.ReadLine());  
  29.   
  30.             string[] temp = arrayTemp.Split(' ');  
  31.   
  32.             int k = 0;  
  33.             for (int i = 0; i < column; i++)  
  34.             {  
  35.                 for (int j = 0; j < rowNO; j++)  
  36.                 {  
  37.                     int OneValue = Convert.ToInt32(temp[k]);  
  38.                     TwoDArray[i, j] = OneValue;  
  39.                     k++;  
  40.                 }  
  41.             }  
  42.             Console.WriteLine("----------------------------------------------");  
  43.             for (int i = 0; i < column; i++)  
  44.             {  
  45.                 if (i == 0)  
  46.                 {  
  47.                     Console.WriteLine("    Column");  
  48.                 }  
  49.   
  50.                 for (int j = 0; j < rowNO; j++)  
  51.                 {  
  52.                     if (j == 0)  
  53.                     {  
  54.                         Console.Write("Rows " + TwoDArray[i, j]);  
  55.                     }  
  56.                     else  
  57.                     {  
  58.                         Console.Write(" " + TwoDArray[i, j]);  
  59.                     }  
  60.   
  61.                 }  
  62.                 Console.WriteLine();  
  63.             }  
  64.         }  
  65.     }  
  66. }  
Input
 
Define Column Number:- 3
Define Row Number:- 3
----------------------------------------------
Now Fill Array:- 1 2 3 4 5 6 7 8 9
 
Output
 
----------------------------------------------
Columns
Rows 1 2 3
Rows 4 5 6
Rows 7 8 9