9

I have a bool array

a[1] = true
a[2] = false
a[3] = true
a[4] = true

how do i select only true values to a new array?

Thanks!

10
  • 2
    What use do you have for an array only containing several copies of the value true? Commented Jan 28, 2011 at 3:58
  • 1
    Check here. This is kind of a duplicate question. Commented Jan 28, 2011 at 3:59
  • 1
    possible duplicate of filter an array in C# Commented Jan 28, 2011 at 4:03
  • 1
    @Artur: It's declared like this: bool[] arrayName; How do I know? Because that's how you declare arrays in C#. I don't see how this is possibly relevant. Commented Jan 28, 2011 at 4:13
  • 1
    @Artur: Then it wouldn't be a "bool array", as explained in the top line. It would be "some other class, with an overridden [] operator and type conversion to bool". Commented Jan 28, 2011 at 4:22

3 Answers 3

13

I don't really know why you would want to do this but...

bool[] a = {true, false, true, true};
bool[] b = a.Where(x => x).ToArray();

If you just want to count how many "true"s there are:

int c = a.Count(x => x);
Sign up to request clarification or add additional context in comments.

5 Comments

You really don't want to get into the habit of performing a boolVar == true check. It's not very good code.
Its explicit so that the questioner can get a better idea of what is happening and hopefully learn. And why is it bad to perform this explicit check. I bet it gets compiled to the exact same thing.
Indeed, the generated IL is the same for both constructs in C#. But that's not necessarily true for all languages. In general, I agree that being explicit is good in your answer, but you might want to add a clarifier that the == true part is not necessary. Sometimes, people just copy and paste code they get in answers here into their IDE.
@AlastairPitts: I don't see any reason to not use the explicit "boolVar == true check," other than to save a few keystrokes. If you think that's bad code, perhaps you should indicate why.
For example, in JavaScript values that would be evaluated a truthy would not pass this equality check. On the other hand, in functional languages where concept of LINQ comes from this check would be a smelly code as well, since it would be more idiomatic to write something like a.Where(Identity), but since the C# hasn't established such function, (x => x) would be in a good proximity.
8

If you mean a new array containing the indices of 'a' that had a value of true...

// Assuming here that a begins at 0, unlike your example...
Enumerable.Range(0, a.Length).Where(i=>a[i]).ToArray();

Comments

4
bool[] result = a.Where(x => x).ToArray();

2 Comments

I think this only works in C# 3.0, where lambda expressions were introduced.
@Timwi: Thanks, I should have been more specific, it works for version of .NET 3.0 and later. ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.