2D Array. Set all values to specific value

前端 未结 6 1173
甜味超标
甜味超标 2021-01-02 13:37

To assign specific value to 1D array I\'m using LINQ like so:

        int[] nums = new int[20];
        nums = (from i in nums select 1).ToArray()         


        
6条回答
  •  孤街浪徒
    2021-01-02 14:14

    LINQ doesn't work particularly well with multi-dimensional arrays.

    Jagged arrays aren't too bad:

    var array = Enumerable.Range(0, 10)
                          .Select(x => Enumerable.Repeat('x', 10).ToArray())
                          .ToArray();
    

    ... but rectangular arrays don't have any specific support. Just use loops.

    (Note the use of Enumerable.Repeat as a somewhat simpler approach to creating the 1-dimensional array, btw.)

提交回复
热议问题