Suggestion: Support Creation of Jagged Array Elements #785
Replies: 8 comments
-
So just to clarify, the syntax: int[][] m = new int[3][4]; would translate into: int[][] m = new int[3][];
m[0] = new int[4];
m[1] = new int[4];
m[2] = new int[4]; I imagine that the concept came up during C# 1.0 and was likely rejected for whatever reason. Perhaps due to the numerous hidden allocations, or just due to a preference to multidimensional arrays. Who knows. This is something easily solved via helper methods, though: public static class ArrayHelpers {
public static T[][] Create<T>(int first, int second) {
var array = new T[first][];
for (int i = 0; i < second; i++) {
array[i] = new T[second];
}
return array;
}
}
I'd expect that with |
Beta Was this translation helpful? Give feedback.
-
FWIW, IL already has first class support for creating jagged arrays in a single IL instruction:
C# doesn't even need to lower this into multiple allocations - the runtime will do them. |
Beta Was this translation helpful? Give feedback.
-
Neat. I wonder if the CLR optimizes such an operation into fewer larger allocations rather than allocating each nested array individually. |
Beta Was this translation helpful? Give feedback.
-
The problem with this syntax is that it is ambiguous with multidimensional arrays, as you point out. Seems weird (and subtle) that identical right hand sides would produce different results depending on whether the left hand side was a multidimensional or jagged array. Even more problematic is that it would impossible to use with |
Beta Was this translation helpful? Give feedback.
-
They're not the same. Both the declaration and allocation of a multidimensional array would use comma syntax. Both the declaration and allocation of jagged arrays would use multiple sets of angle brackets: int[,] a1 = new int[3, 4]; // multidimensional array
int[][] a2 = new int[3][4]; // jagged array
var a3 = new int[3, 4]; // multidimensional array
var a4 = new int[3][4]; // jagged array |
Beta Was this translation helpful? Give feedback.
-
Depending on memory fragmentation this could be a bad thing. I've had more luck allocating an array of arrays in memory-intensive apps than multidimensional arrays. |
Beta Was this translation helpful? Give feedback.
-
I would just write a linq for that kind of functionality int[][] arr = Enumerable.Range(0,10).Select((i) => new int[4]).ToArray();
// range style, 1.To(10) yield 1,2,3,4,5,6,7,8,9,10
int[][] arr = 1.To(10).Select((i) => new int[4]).ToArray(); On the other hand I would like to have multi-dimensional array from linq |
Beta Was this translation helpful? Give feedback.
-
@HaloFour Quite right; thanks. The original post was worded confusingly. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently "int[][] m = new int[3][4];" is an invalid statement. Maybe C# should support this syntax like Java. Although C# has multidimensional arrays like "int[,] m = new int[3][4];", but its elements are int not int[]. It's not convenient to iterate the "sub array" or convert it to "a list of array".
Beta Was this translation helpful? Give feedback.
All reactions