So I came across some code of mine from an old VSTO project, and noted this little 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 specifying [,] on a datatype mean? Looking at the code it appears to function as a matrix, but honestly I thought c# matrices were handled with notation like object[ ][ ].
object[,]refers to a rectangular array, that means it’s a grid.Then you have
object[][]which is a jagged array, an array of array.The main difference is that
object[,]will always have fixed dimensions, while with a jagged array (object[][]) all arrays can have different sizes.This is an example that clearly shows the difference in usage (both do the same):
EDIT:
Warning, this code above is not real production code, it’s just the clearest way of doing it as example.
The intensive use of divisions through
/and%makes it much slower. You could better use a nested for loop.