Two-dimensional arrays in C#

Before perusing ahead, I would prescribe to peruse: One (Single) Dimensional Arrays in C#.

In Single Dimensional Array, we had the option to store components in a solitary measurement (Array components were store bordering).

On the off chance that we have to store information in an unthinkable like the arrangement, we can’t do this utilizing single (one) dimensional cluster.

In two dimensional exhibits, we can store more than one measurement for clusters in C#. Two-dimensional exhibit stores information in the unthinkable structure.

Here, the first measurement indicates a number of lines and the second determine the number of sections.

Sentence structure of single-dimensional exhibit:

<data_type>[,] variable_name = new <data_type>[SIZE]; 

Program

using System;

namespace arrayEx
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 0;
            int j = 0;
            int[,] X;

            X = new int[2,3];

            Console.Write("Enter Elements : \n");
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.Write("\tElement[" + i + ","+j+"]: ");
                    X[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }

            Console.Write("\n\nElements are: \n");
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 3; j++)
                {
                    Console.Write(X[i, j] + " ");
                }
                Console.WriteLine();
            }

        }
    }
}

Output

Enter Elements :      
        Element[0,0]: 10
        Element[0,1]: 20
        Element[0,2]: 30
        Element[1,0]: 40
        Element[1,1]: 50
        Element[1,2]: 60
 
 
Elements are:         
10 20 30              
40 50 60
Press any key to continue . . .

Leave a Comment

error: Alert: Content is protected!!