33

I'm already aware of the loop example below

bool[] switches = new bool[20]; for (int i = 0; i < switches.Length; i++) { switches[i] = false; } 

But is there a more efficient way to set the entire array to false?

To explain myself a bit, i'm not, like the example above, setting a new bool array to false as it would already be false anyway. In my case i'm reading a large portion of a process memory which could fire about 18724 times at most as it is searching for patterns. When it determines that the current bytes doesn't contain the pattern, it sets the entire bool array to false along with a few other things then reads the next memory block and restarts the loop.

Although its more out of curiosity that I asked this question because the whole process still takes less than a second.

So again, my question is, is there a better way to set the entire bool array to false?

3
  • 10
    Just assign it to a new array: switches = new bool[20], by default all items will be false. Although this won't work if references to the array are handed out to other places, as this only affects the reference you set. No idea if it's faster or not, and does potentially put pressure on GC. Commented Dec 13, 2013 at 11:56
  • 2
    Almost certainly not. The process of the loop is going to need to be done regardless. Commented Dec 13, 2013 at 11:56
  • 1
    @user2025312: There are definite possibilities for optimization. Commented Dec 13, 2013 at 12:33

6 Answers 6

70

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

bool[] switches = new bool[20]; 
Sign up to request clarification or add additional context in comments.

1 Comment

And what if you create a Bool[] and make all of them as True.
21

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(); 

1 Comment

Worth noting that an Enumerable.Repeat is pretty slow.
11

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.

Comments

4

I believe the default value of a boolean is false. However as a sanity check, it makes sense to iterate through it. AFAIK, that is the fastest way

See: How to populate/instantiate a C# array with a single value?

Comments

3

Is there a better way to set the entire bool array to false?

No, there is not.

You could benchmark, if assigning a new array is faster, but I doubt it. Of course, this would be done as pointed out by Adam Houldsworth.

switches = new bool[20]; 

10 Comments

If there is no better way what about Array.Clear()?
What do you think will Array.Clear() do in the background? ;-)
@FabianBigler To answer your reply in the deleted answer, it was a tongue-in-cheek comment. You should definitely not use linq just because you can lol
@AdamHouldsworth Ah well in the written language it's sometimes hard to catch the irony. ;-)
@AdamHouldsworth And also, I did not downvote your answer anyway. ;-)
|
2

switches.Clear(); will do the work. Arrays in .NET have fixed size, that's why Clear() will not remove any element, it will set all elements to default values instead (for bool this is false)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.