Multi Dimentional Arrays


using System;

class MultiDimArray
{
    static void Main(string[] args)
    {
        string[,] myArray;                                                          // Declaring a multi dimensional string array

        myArray = new string[2, 2] { { "John", "Peter" }, { "James", "ALbert" } };   // Initializing the array

        // or we can do the initialization during declaration itself as follows.
        string[,] newArray = new string[2, 2] { { "John", "Peter" }, { "James", "ALbert" } };

        //iterating the array
        Console.WriteLine("Contents of string array: ");
        foreach (string name in newArray)
        {
            Console.Write(name + ",");
        }

        // If we don't want to specify the size of arrays, just don't define a number when we call new operator.
        int[,] numArray = new int[,] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };
        Console.WriteLine(" " + "Contents of integer array: ");
        foreach (int i in numArray)
        {
            Console.Write(i + ",");
        }

        //We can also omit the new operator and assign these values directly without using the new operator
        int[,] numbers = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } };

        Console.ReadLine();
    }
}

Output:
Contents of string array:
John,Peter,James,ALbert,
Contents of integer array: 
1,2,3,4,5,6,7,8,