0

I'd like to declare an array of int rows with a variable size (X) and init all values to 1. For the moment I use this :

int[] rows = new int[X];
for (int i = 0; i < rows.Length; i++)
{
   rows[i] = 1;
}

Is there any faster/shorter way to do it with some sort of fill(1) or int[] rows = new int[X] {1}; ?

0

1 Answer 1

1

LINQ:

int[] rows = Enumerable.Repeat(element:1, count: X).ToArray();// named parameter -  X
                                                              // doesn't tell anything
Sign up to request clarification or add additional context in comments.

1 Comment

As Eric Lippert notes, doing this is tens of times slower than simply writing a loop (it actually is).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.