user3648426
user3648426

Reputation: 251

Loop through 2D array and print results with format

I have a simple 2D array that repeats the name of a staff and their corresponding department.

string[,] staffArray= new string[,] { { "StaffID", "Dept" }, { "StaffID", "Dept" }, { "StaffID", "Dept" }, { "StaffID", "Dept" } };

I want to print it back to back to get "Staff, Dept" as a result. However, I would like to do this via loop.

        Console.Write(staffArray[0, 0] + ", ");
        Console.WriteLine(staffArray[0, 1]);
        Console.Write(staffArray[1, 0] + ", ");
        Console.WriteLine(staffArray[1, 1]);

Prints:

Staff, Dept

Staff, Dept

Can someone explain how I could achieve this result with a loop?

Upvotes: 0

Views: 54

Answers (1)

Marius George
Marius George

Reputation: 526

     string[,] multiPropertySelect = new string[,] { { "StaffID", "Dept" }, { "StaffID", "Dept" }, { "StaffID", "Dept" }, { "StaffID", "Dept" } };

     for (int x = 0; x < multiPropertySelect.GetLength(0); ++x)
     {
        Console.WriteLine(string.Format("{0}, {1}", multiPropertySelect[x, 0], multiPropertySelect[x, 1]));
     }

     Console.ReadKey();

Upvotes: 4

Related Questions