2D Array. Set all values to specific value

前端 未结 6 1171
甜味超标
甜味超标 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:12

    One way you could do this is like so:

    // Define a little function that just returns an IEnumerable with the given value
    static IEnumerable Fill(int value)
    {
        while (true) yield return value;
    }
    
    // Start with a 1 dimensional array and then for each element create a new array 10 long with the value of 2 in
    var ar = new int[20].Select(a => Fill(2).Take(10).ToArray()).ToArray();
    

提交回复
热议问题