What does the object [,] in C # mean?

So, I came across some code from the old VSTO project and noted this a bit:

Excel.Worksheet sheet = Globals.ThisAddIn.Application.Worksheets["Unique Hits Per URL"]; Dictionary<int, string> ids = new Dictionary<int, string>(); object[,] cellRange = (object[,])sheet.get_Range("E:E").Cells.Value; for (int i = 1; i < cellRange.GetUpperBound(0); i++) if (cellRange[i, 1] != null) ids.Add(i, cellRange[i, 1].ToString()); 

What does [,] indicate a data type? Looking at the code, it seems to function as a matrix, but to be honest, I thought that C # matrices are handled using notations such as object [] [].

+6
arrays c # matrix excel vsto
source share
4 answers

object[,] refers to a rectangular array, which means its grid.
Then you have object[][] , which is an array with notches, an array of arrays.

The main difference is that object[,] will always have fixed sizes, and with an uneven array ( object[][] ), all arrays can have different sizes.

This is an example that clearly shows the difference in usage (both do the same):

 // Create and fill the rectangluar array int[,] rectangularArray = new int[10, 20]; for (int i = 0; i < 200; i++) rectangularArray[i / 20, i % 20] = i; // Next line is an error: // int[][] jaggedArray = new int[10][20]; int[][] jaggedArray = new int[10][]; // Initialize it // Fill the jagged array for (int i = 0; i < 200; i++) { if (i % 20 == 0) jaggedArray[i / 20] = new int[20]; // This size doesn't have to be fixed jaggedArray[i / 20][i % 20] = i; } // Print all items in the rectangular array foreach (int i in rectangularArray) Console.WriteLine(i); // Print all items in the jagged array // foreach (int i in jaggedArray) <-- Error foreach (int[] innerArray in jaggedArray) foreach (int i in innerArray) Console.WriteLine(i); 

EDIT:
Warning, this code above is not real production code, it is simply the best way to do this as an example.
Intensive use of divisions through / and % makes it much slower. You can better use a nested loop loop.

+16
source share

In C #, there are two different types of "multidimensional arrays."

T[,] - Multidimensional array . T[][] is a jagged array .

The main difference is how they are stored. Multidimensional arrays are stored as a single continuous block of memory. Hard arrays are an array of arrays. Thus, for multidimensional arrays only one selection is required, and each "row" and "column" will have the same size (they are always IxJ).

In an array with a serrated contour, the elements of the β€œsecond” array can have different lengths, since they are separate arrays. It stores an array of links, each of which can point to a separate array of elements.

At the same time, contrary to general expectations, jagged arrays, despite the fact that they occupy more memory (for storing additional references to arrays) and are not stored adjacent to the memory, often perform better than multidimensional arrays due to some optimizations in the CLR environment.

+4
source share

object[,] refers to an array of objects with two dimensions.

In general, 2-dimensional arrays can be thought of as tables, where some dimensions represent columns and another index represents rows.

 int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} }; 0 1 ------ 0 | 1 2 1 | 3 4 2 | 4 6 

Check out the part about multidimensional arrays here:
http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

+3
source share

This is a matrix or two dimensional arrays. MSDN doc here

+2
source share

All Articles