0

I'm a programming newbie and I just started learning about arrays. What confuses me is the way one can initialize a multidimensional array in C#. And that is because I don't specify the coordinates of each element myself, but C# does it somehow.

More specifically, let's say that I initialize this array:

string[,] names = {
 {"Deadpool", "Superman", "Spiderman"}
 {"Catwoman", "Batman", "Venom"}
};

Given that in single dimensional array initialization C# starts by placing the first value in element [0], I assume that the same holds true for multi-dimensional arrays too. But doesn't it do the same for each seperate dimension ?This can't be true. Because then it would place "Deadpool" on element [0] of the row, and later it would try to place "Catwoman" on element [0] of the first column. And if that was the case, given that element [0] of the first row is the same element [0] of the first column, "Catwoman" would replace "Deadpool" as both would be written in [0,0]. Yet using a foreach to write everything on the console, both show up. So how and where does C# place each value, to avoid a value of one dimension overwriting the value of another dimension ?

  • 1
    Well... There are two groups of data items enclosed in braces, so there's no room for ambiguity. The compiler does the right thing. – Lucas Trzesniewski Apr 03 '16 at 19:04
  • Welcome to Stackoverflow. You could just loop through your array and you would see. All documented here: https://msdn.microsoft.com/en-us/library/9b9dty7d(v=vs.140).aspx – Quality Catalyst Apr 03 '16 at 19:07

1 Answers1

0

If you convert the foreach to a for loop it might mack a little bit more sense:

static  void Main(string[] args)
       {
           string[,] names = {{"Deadpool", "Superman", "Spiderman"},{"Catwoman", "Batman", "Venom"}};

           for (int index0 = 0; index0 < names.GetLength(0); index0++)
           {
               for (int index1 = 0; index1 < names.GetLength(1); index1++)
               {
                   var name = names[index0, index1];
                   Console.Write(name);
               }
           }
       }

This is essentially what the foreach is doing behind the scenes. If you want to learn how that actually works start here:

How does the Java 'for each' loop work?

How is foreach implemented in C#?

How do foreach loops work in C#?

Community
  • 1
  • 1
Wjdavis5
  • 3,952
  • 7
  • 35
  • 63
  • Oh I see. This example helped me understand that the procedure of placing values to elements start from 0 only for the first dimension. The values of the second dimension are starting from 1, skipping the first row, so "Deadpool" ended up in [0,0] and "Catwoman" in [1,0]. Thanks! –  Apr 03 '16 at 19:31
  • My pleasure - if this answered your question please remember to mark it as the answer. – Wjdavis5 Apr 03 '16 at 19:31
  • I did, yet let me ask you something more. Adding yet another dimension, and another for loop with index2, when I change the line `var name = names[index0, index1]` to var name = [index0, index1, index2], I get an error that I can only add two indices inside []. Why is that ? –  Apr 03 '16 at 19:34