对象[,]在c#中意味着什么?

时间:2022-04-21 16:56:33

So I came across some code of mine from an old VSTO project, and noted this little bit:

所以我从一个旧的VSTO项目中看到了我的一些代码,并注意到这一点:

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

在数据类型上指定[,]意味着什么?看看代码它看起来像一个矩阵,但老实说我认为c#矩阵是用object [] []这样的符号来处理的。

4 个解决方案

#1


16  

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.

object [,]指的是一个矩形数组,这意味着它是一个网格。然后你有object [] []这是一个锯齿状数组,一个数组数组。

The main difference is that object[,] will always have fixed dimensions, while with a jagged array (object[][]) all arrays can have different sizes.

主要区别在于object [,]将始终具有固定的维度,而对于锯齿状数组(object [] []),所有数组都可以具有不同的大小。

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.

编辑:警告,上面的代码不是真正的生产代码,它只是最简单的方式来做它的例子。通过/和%大量使用分区会使速度慢得多。您可以更好地使用嵌套for循环。

#2


4  

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

C#中有两种不同类型的“多维数组”。

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

T [,]是一个多维数组。 T [] []是一个锯齿状的数组。

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

主要区别在于它们的存储方式。多维数组存储为单个连续的内存块。锯齿状数组实际上是一个数组数组。因此,多维数组只需要一个分配,每个“行”和“列”将具有相同的大小(它们总是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.

话虽如此,与常见的期望相反,即使它们需要更多内存(用于存储额外的数组引用)并且没有连续存储在内存中,但由于CLR中的一些优化,它们通常比多维数组执行得更好。

#3


3  

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

object [,]指的是具有两个维度的对象数组。

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

在这里查看有关多维数组的部分:http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

#4


2  

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

它是一个矩阵或二维数组。这里是MSDN doc

#1


16  

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.

object [,]指的是一个矩形数组,这意味着它是一个网格。然后你有object [] []这是一个锯齿状数组,一个数组数组。

The main difference is that object[,] will always have fixed dimensions, while with a jagged array (object[][]) all arrays can have different sizes.

主要区别在于object [,]将始终具有固定的维度,而对于锯齿状数组(object [] []),所有数组都可以具有不同的大小。

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.

编辑:警告,上面的代码不是真正的生产代码,它只是最简单的方式来做它的例子。通过/和%大量使用分区会使速度慢得多。您可以更好地使用嵌套for循环。

#2


4  

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

C#中有两种不同类型的“多维数组”。

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

T [,]是一个多维数组。 T [] []是一个锯齿状的数组。

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

主要区别在于它们的存储方式。多维数组存储为单个连续的内存块。锯齿状数组实际上是一个数组数组。因此,多维数组只需要一个分配,每个“行”和“列”将具有相同的大小(它们总是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.

话虽如此,与常见的期望相反,即使它们需要更多内存(用于存储额外的数组引用)并且没有连续存储在内存中,但由于CLR中的一些优化,它们通常比多维数组执行得更好。

#3


3  

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

object [,]指的是具有两个维度的对象数组。

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

在这里查看有关多维数组的部分:http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

#4


2  

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

它是一个矩阵或二维数组。这里是MSDN doc