Michael
Michael

Reputation: 1622

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

Upvotes: 7

Views: 885

Answers (4)

Aidiakapi
Aidiakapi

Reputation: 6249

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.

Upvotes: 16

ntziolis
ntziolis

Reputation: 10231

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

In general 2 dimensional arrays can be seen as tables where the one dimensions represents the columns and the other index represents the rows.

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

    0  1
   ------
0 | 1  2
1 | 3  4
2 | 4  6

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

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564631

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.

Upvotes: 4

Arve
Arve

Reputation: 7516

It is a matrix or a two dimentional array.MSDN doc here

Upvotes: 2

Related Questions