Setting entire bool[] to false [duplicate] Setting entire bool[] to false [duplicate] arrays arrays

Setting entire bool[] to false [duplicate]


default(bool) is false, just create the array and each element will be false.

bool[] switches = new bool[20];


If you can re-init array, then answer of Fabian Bigler is the right way to go.

If, however, you want to re-init it to true, then use Enumerable.Repeat:

switches = Enumerable.Repeat(true, 20).ToArray();


You can try Array.Clear as an alternative:

Array.Clear(switches, 0, switches.Length);

Not sure what the relative performance will be, but you should definitely not expect miracles.