Introduction:

Here we will explain how to use multi dimensional array in c# or what is multi dimensional array in c# or How to access value of multi dimensional array in c# with example.

 

Description:

The multi-dimensional array in C# store more than one row data.it can be two or three or more dimensional array.It contains more than one coma (,) within single rectangular brackets (“[ , , ,]”).. To accessing the value from multi dimensional array, you need to use nested loop in c# program. The following example will help you to know clearly the concept of multi dimensional array.

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Multi_Dimensional_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            //Declaring multi dimensional array
            string[,] Items = new string[2, 2];
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    Console.Write("\nEnter Book Name for {0}. Row and {1}. column:\t", i + 1, j + 1);
                    Items[i, j] = Console.ReadLine();
                }
            }
 
            Console.WriteLine("\n\n=========================");
            Console.WriteLine("All the element of Books array is:\n\n");
 
            //Formatting Output
            Console.Write("\t1\t2\n\n");
            //outer loop for accessing rows
            for (i = 0; i < 2; i++)
            {
                Console.Write("{0}.\t", i + 1);
 
                //inner or nested loop for accessing column of each row
                for (j = 0; j < 2; j++)
                {
                    Console.Write("{0}\t", Items[i, j]);
                }
                Console.Write("\n");
            }
            Console.WriteLine("\n\n=========================");
            Console.ReadLine();
        }
    }
}

 

Output:



I hope this article will help to you.

0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

Welcome to SQL Tutorials Point

Don't have an account? Click here to Create a new account, it takes less than a minute.