double[][][] threeDimensionArray =
Enumerable.Range(0, 1)
.Select(h1 => Enumerable.Range(0, 2)
.Select(h2 => new double[3])
.ToArray())
.ToArray();
ただし、これにはToArray()
メモリコピーを実行する複数の呼び出しが必要であるため(以下の実装を参照)、多数のアイテムの場合は効率的ではないため、このような「エレガントな」ソリューションは無料ではありません。ところで、私はfor
ループソリューションを好みます。
Enumerable.ToArray()
実装:( 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;
}