Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does object[,] mean in c#?

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[ ][ ].

like image 460
Michael Avatar asked Mar 31 '11 17:03

Michael


2 Answers

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):

// 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'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.

like image 145
Aidiakapi Avatar answered Sep 21 '22 15:09

Aidiakapi


There are two different types of "multidimensional arrays" in C#.

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

The main difference is in how they are stored. Multidimensional arrays are stored as a single, contiguous chunk of memory. Jagged arrays are effectively an array of arrays. As such, multidimensional arrays only need a single allocation, and every "row" and "column" will be the same size (they're always IxJ).

In a jagged array, the "second" array elements can all be a different length, since they're individual arrays. It's storing an array of references, each of which can point to a separate array of elements.

That being said, contrary to common expectations, jagged arrays, even though they take more memory (to store the extra array references) and are not stored contiguously in memory, often perform better than multidimensional arrays due to some optimizations in the CLR.

like image 38
Reed Copsey Avatar answered Sep 18 '22 15:09

Reed Copsey