3

I'm very new to C#. I was curious on how this block of code printed out 3 separate lines with the 3 words in the arrays. Could someone explain how it works?

using System;

namespace MyFirstProgram
{
    class Program 
    {
        static void Main(string[] args)
        {

            String[,] parkingLot = {{ "Mustang", "F-150", "Explorer" }, { "Corvette", "Camaro", "Silverado" }, { "Corolla", "Camry", "Rav4" }};

            for(int i = 0; i < parkingLot.GetLength(0); i++)
            {
                for (int j = 0; j < parkingLot.GetLength(1); j++)
                {
                    Console.Write(parkingLot[i, j] + " ");
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}```
  • If your array was declared as a `string[][] parkingLot` and the loops were `for(int i = 0; i < parkingLot.Length; i++)` and `for (int j = 0; j < parkingLot[i].Length); j++)` would you understand it then? – Caius Jard Feb 12 '22 at 07:28
  • Side note, in C# we probably wouldn't do this; we'd perhaps define a class CarMaker that had properties like Brand and Models (plural, list of string) and then have a list of CarMaker. We would write loops like `foreach(var carmaker in carmakers) foreach(var model in carmaker.Models)` - I get that this is probably programming 101, but when this course module is done aim to avoid storing nameless data in arrays like this, as it might lead you to think it's a good idea to have another array of strings with 3 car brand names in.. – Caius Jard Feb 12 '22 at 07:33

2 Answers2

3

The GetLength() method returns the size of the dimension passed in:

Gets a 32-bit integer that represents the number of elements in the specified dimension of the Array.

Notice the first call gets passed a zero:

parkingLot.GetLength(0)

This returns the number of ROWS in the 2D array.

The second call is passed a one:

parkingLot.GetLength(1)

Which tells you the number of COLUMNS in the 2D array.

Perhaps this example will illustrate the concept better:

String[,] parkingLot = {
  { "a", "b", "c" },
  { "1", "2", "3" },
  { "red", "green", "blue" },
  { "cat", "dog", "fish"},
  { "A1", "B2", "C3"}
};

for(int row = 0; row < parkingLot.GetLength(0); row++) // 5 rows
{
  for (int col = 0; col < parkingLot.GetLength(1); col++) // 3 columns
  {
    Console.Write(parkingLot[row, col] + " ");
  }
  Console.WriteLine();
}

Output:

a b c 
1 2 3 
red green blue 
cat dog fish 
A1 B2 C3 
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
2

I'm going to break this down into each line of the code, and explain what it does.

String[,] parkingLot = { { "Mustang", "F-150", "Explorer" }, { "Corvette", "Camaro", "Silverado" }, { "Corolla", "Camry", "Rav4" } };

parkingLot is a multi-dimensional string Array. This means this is a string array that hold other arrays of type string. Think of each {} within the bigger {} its own Array object that can be accessed.

for (int i = 0; i < parkingLot.GetLength(0); i++)

For loops are designed to loop through a specified number of elements. the integer "i" created at the start indicates that the loop will start at index 0. We start at 0 because array indexing starts at 0.

The duration of this loop lasts as long as the number of arrays ({}) in the initialised array. In this case, because parkingLot contains 3 arrays, the length is 3.

for (int j = 0; j < parkingLot.GetLength(1); j++)

This part of the code iterates within the for-loop above it, and iterates over every element inside a single array. The "GetLength(1)" part of the code grabs the length of a single array instance within the overall array. For example: the first array instance in this multi-dimensions array contains { "Mustang", "F-150", "Explorer" }, being 3 strings. Therefore, the length will also be 3.

Console.Write(parkingLot[i, j] + " ");

Here, we can see that there's a reference to i and j. Let me show you what happens step by step:

when i=0, the code begins to iterate another loop, which must compute j = 0, j = 1 and j = 2 before moving to i = 1. This means that it must read every element in the array before moving to the next array.

For Example: say:

  • i = 0, and j = 0. this would be accessing "Mustang"
  • i = 1, and j = 2 would be accessing "Silverado"
  • i = 2 and j = 1 would be accessing "Camry"
Console.WriteLine();

After writing each element in the j loop, this piece of code writes the responses to the console. That's why each elements of arrays appear on one line.

Console.ReadKey();

After you press this key, the program closes.

I suggest you play around with the variables and create your own arrays and multi-dimensional arrays. That way you can see how they operate and what they don't like you doing with them.