x


Array Of Bools

Hello, I was wondering if it was possible to hold an array of booleans and then be able to check if they are false or not in C#. What i wanted to do is have an if statement that says if there all false, then set one true. The thing is im not very familiar with arrays so can someone point me in the right direction for what im looking for?

Thanks For The Help :)

more ▼

asked Feb 21 '12 at 03:49 PM

TripodGRANNE gravatar image

TripodGRANNE
101 10 12 14

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

just use a for loop:

bool allFalse = true;
bool[] array;
for(int i = 0; i < array.length; i++)
  if(!allFalse) break; // allow the loop to end premature
  else allFalse = ! array[i];
if(allFalse)
  array[0] = true;

is that what you want to do?

more ▼

answered Feb 21 '12 at 03:58 PM

ZweiD gravatar image

ZweiD
51 1 2 3

It gives me the error "bool[] does not contain a definition for 'length' and no extension method 'length' of type bool[] could be found"

Then it says "are you missing a directive or an assembly reference"

Feb 21 '12 at 04:21 PM TripodGRANNE

Yes, it should be array.Length (capitalized).

Feb 21 '12 at 04:45 PM ti89TProgrammer

Awesome it works now, thanks for the help guys :)

Feb 21 '12 at 04:48 PM TripodGRANNE

oh right, sorry for that ^.^

now everybody knows that i am a java guy ;)

glad it worked

Feb 21 '12 at 05:57 PM ZweiD
(comments are locked)
10|3000 characters needed characters left

This is quite a good tutorial for arrays in C#. See if it helps you.

MSND C# Tutorial

more ▼

answered Feb 21 '12 at 05:56 PM

HazeTI gravatar image

HazeTI
346 9 14 17

(comments are locked)
10|3000 characters needed characters left

The easiest thing to do is use LINQ. (using System.Linq; at the top of your code):

bool[] array;
if (array.All(b => !b)) //do whatever

http://msdn.microsoft.com/en-us/library/bb548541.aspx

more ▼

answered Feb 21 '12 at 05:21 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

(comments are locked)
10|3000 characters needed characters left

You can use one of a couple of methods in the System.Array class: !Array.Exists(array, item => item) or Array.TrueForAll(array, item => !item). Alternatively, if you put using System.Linq; at the top of the file, then you can use either array.All(item => !item) or !array.Any(item => item).

The list of booleans more compactly as a BitArray object (defined in the System.Collections namespace), in which case either of those last two methods may be used; just replace the array with bitArray.Cast<bool>().

However, if you don’t need to store more than 32 values in the list, then you can achieve greater efficiency by scrapping the use of an actual array completely and instead using an instance of the BitVector32 structure (in the System.Collections.Specialized namespace); this enables replacing the whole loop with a single constant-time statement:

allFalse = (bitVector.Data == 0);

In this case, if you need to store fewer than 32 values, then you might need an extra variable to store the logical count of booleans.

more ▼

answered Feb 21 '12 at 10:21 PM

ti89TProgrammer gravatar image

ti89TProgrammer
66 1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4165
x3739
x1363
x240

asked: Feb 21 '12 at 03:49 PM

Seen: 916 times

Last Updated: Feb 22 '12 at 12:13 AM