Initialize a Jagged Array the LINQ Way

后端 未结 6 2212
孤城傲影
孤城傲影 2020-12-14 04:07

I have a 2-dimensional jagged array (though it\'s always rectangular), which I initialize using the traditional loop:

var myArr = new double[rowCount][];
for         


        
6条回答
  •  孤城傲影
    2020-12-14 04:49

    The behavior is correct - Repeat() returns a sequence that contains the supplied object multiple times. You can do the following trick.

    double[][] myArr = Enumerable
        .Repeat(0, rowCount)
        .Select(i => new double[colCount])
        .ToArray();
    

提交回复
热议问题