I have an intriguing question. considering this line of code :
List<bool> selectedValues = allValues.FindAll(x => x);
where allValues is a list of booleans.
Why is that FindAll() finds all true values in selectedValues list but not all the false one?
To my knowledge, we are assessing the same value in (x => x). So if you assess False and False (or False goes to False), shouldn't that be true as well?
why is it that if x = true, (x=>x) == true, but if x = false, (x=>x) == false.
my understanding is that :
- false equals false (that makes it true)
- true equals true (that makes it true as well) Why is it that 1) is not true
I rewrote the same syntax as :
List<bool> selectedHobbies = allHobbies.FindAll(hobby => x.Equals(true));
The second one is more understanding to me. But I cannot get a grip on the first syntax.
because the second syntax actually checks x with another value
In
x => x, you are essentially evaluating the expressionx, like you would have done forx.Equals(true). Therefore, it will only pick the values where this expression evaluates to true i.e. all true values in your collection.