Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize runtime value-type array with specific value?

Tags:

arrays

c#

.net

Is there a way I can initialize the following runtime array to all trues without looping over it using a foreach?

Here is the declaration:

bool[] foo = new bool[someVariable.Count];

Thanks!

like image 445
Polaris878 Avatar asked Dec 06 '25 20:12

Polaris878


2 Answers

bool[] foo = Enumerable.Repeat(true, someVariable.Count)
                       .ToArray();
like image 134
Ani Avatar answered Dec 08 '25 09:12

Ani


bool[] bools = (new bool[someVariable.Count]).Select(x => !x).ToArray();

Sort of kidding. Kind of. Almost.

like image 30
Deniz Dogan Avatar answered Dec 08 '25 11:12

Deniz Dogan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!