C# > Arrays

Two dimensional arrays in C#

How to use two dimensional arrays?


Arrays which are having more than one value are called two dimensional arrays. The two dimensional arrays are defined as which are having two columns are called two dimensional arrays.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
  {
   static void Main()
    {
     String [,] website2D = new string[3, 2]
       {
          {"DotnetFunda", "ITFunda"}, {"TechFunda", "FarmingFunda" }, {"KidsFunda", "FundooVideo" }
       }; // Two dimensional array with fixed size..

            Console.WriteLine(website2D[1, 0]);  // TechFunda : 0th index and 0th element
            Console.WriteLine(website2D[0, 1]);  // ITFunda : 1st index and 1st element
            Console.WriteLine(website2D[1, 1]);  // FarmingFunda : 2nd index and 0th element
            Console.WriteLine(website2D[2, 0]);  // KidsFunda : 2nd index and 1st element
     }
  }     
 

In the above code snippet we have declared Two dimensional arrays, We are giving string name as websites. Consider these three strings { {dotnetfunda,itfunda},{techfunda,farming funda},{kids funda,fundoo video} } as (0,1,2) indexes, the two elements { {dotnetfunda,itfunda} } as (0,1). In the first line we are calling [1,0] as 1st index and 0 element and prints the output as Techfunda. In the second line we are calling [0,1] as 0 index and 1 st element as ITFunda. In the third line we are calling [1,1] as 1 st index and 1st element and prints the output as Farmingfunda. In the fourth line we are calling [2,0] as second index and 0 th element and prints the value as Kidsfunda.

output:

 Views: 916 | Post Order: 11



Write for us