简体   繁体   中英

C# Array Initialization on []

So in C# you can define an array like so:

string[] Demo;
string[,] Demo;
string[,,] Demo;

What does the , represent?

The dimensions.

  • No comma: 1 dimension
  • 1 comma: 2 dimensions
  • 2 commas: 3 dimensions
  • and so on...

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 (eg string[,] ), which are like a matrix :

+-+-+-+-+
| | | | |
+-+-+-+-+
| | | | |
+-+-+-+-+
| | | | |
+-+-+-+-+

and jagged arrays (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM