double[][][] threeDimensionArray =
Enumerable.Range(0, 1)
.Select(h1 => Enumerable.Range(0, 2)
.Select(h2 => new double[3])
.ToArray())
.ToArray();
But this requires multiple ToArray()
calls which doing memory copy (see implementaiton below) so for big number of items it would not be efficient so such kind of "elegant" solution is not for free. BTW, I would prefer for
loop solution.
Enumerable.ToArray()
implementation: (credits to ILSpy)
// System.Linq.Enumerable
public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
// sll: see implementation of Buffer.ToArray() below
return new Buffer<TSource>(source).ToArray();
}
// System.Linq.Buffer<TElement>
internal TElement[] ToArray()
{
if (this.count == 0)
{
return new TElement[0];
}
if (this.items.Length == this.count)
{
return this.items;
}
TElement[] array = new TElement[this.count];
Array.Copy(this.items, 0, array, 0, this.count);
return array;
}