So in C# you can define an array like so:
string[] Demo;
string[,] Demo;
string[,,] Demo;
What does the ,
represent?
So in C# you can define an array like so:
string[] Demo;
string[,] Demo;
string[,,] Demo;
What does the ,
represent?
The dimensions.
Learn more about multi-dimensional arrays on MSDN.
Multi-dimensional arrays.
The following example would declare a string array with two dimensions:
string[,] demo = new string[5, 3];
The [,]
syntax is useful, for example, if you have a method taking a 2D array as a parameter:
void myMethod(string[,] some2Darray) { ... }
Note the difference between multi-dimensional arrays (e.g. string[,]
), which are like a matrix:
+-+-+-+-+
| | | | |
+-+-+-+-+
| | | | |
+-+-+-+-+
| | | | |
+-+-+-+-+
and jagged arrays (e.g. string[][]
), which are basically arrays of arrays:
+------------+
| +-+-+-+-+ |
| | | | | | |
| +-+-+-+-+ |
+------------+
| +-+-+-+-+ |
| | | | | | |
| +-+-+-+-+ |
+------------+
| +-+-+-+ |
| | | | | | <- possible in jagged arrays but not in multi-dimensional arrays
| +-+-+-+ |
+------------+
Reference:
These are multi-dimensional arrays.
The difference between this and array[][]
as you might be used to is described here and here