Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TripodGRANNE · Feb 21, 2012 at 03:49 PM · c#arrayboolean

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 :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by ZweiD · Feb 21, 2012 at 03:58 PM

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?

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TripodGRANNE · Feb 21, 2012 at 04:21 PM 0
Share

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"

avatar image ti89TProgrammer · Feb 21, 2012 at 04:45 PM 2
Share

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

avatar image TripodGRANNE · Feb 21, 2012 at 04:48 PM 0
Share

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

avatar image ZweiD · Feb 21, 2012 at 05:57 PM 0
Share

oh right, sorry for that ^.^

now everybody knows that i am a java guy ;)

glad it worked

avatar image
0

Answer by HazeTI · Feb 21, 2012 at 05:56 PM

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

MSND C# Tutorial

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Jessy · Feb 21, 2012 at 05:21 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ti89TProgrammer · Feb 21, 2012 at 10:21 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Saving 30 bools in an array possible? 2 Answers

How do I make ALL other booleans in a bool array false when one is true ? 2 Answers

Distribute terrain in zones 3 Answers

question about array or list of boolean 1 Answer

Initialize Array custom datatype 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges