2

I have three boolean variables. I need to check if all three are true or all three are false.

I can do this in a 'dummy' way:

bool areSame = false; 
if(a && b && c)    
    areSame = true; 
else if (!a && !b && !c)
    areSame = true;

I'd like to know if there is another more elegant solution.

0

7 Answers 7

7

You can use the equality operator on booleans too:

bool areSame = (a == b) && (a == c);
3
  • 4
    Why settle for readable code when you can throw in bitwise operands instead? var areSame = !(a ^ b | b ^ c); :D Commented Jun 29, 2012 at 14:47
  • @ChrisSinclair: I sometimes go for bitwise operations for efficiency (e.g. to multiply or divide by powers of 2), but I doubt there will be any performance benefits here.
    – Douglas
    Commented Jun 29, 2012 at 15:09
  • yeah, I was just joking around anyway. There's no way I'd use bitwise operations for a simple check like this. If it was just to check that all values were true then I'd say "go to it!" with bool areSame = a & b & c & d & e but the all false option kinda throws a monkey wrench into that. Commented Jun 29, 2012 at 15:34
6

Sure, you're only comparing three here, but what about the future? You might have to compare ten booleans to each other somewhere down the road.

    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(false.AllEqual(false, false)); 
                Console.WriteLine(true.AllEqual(false, false));
                Console.WriteLine(true.AllEqual(true, false));

                bool a = true;
                bool b = false;
                bool c = true;
                Console.WriteLine(a.AllEqual(b, c));
                b = true;
                Console.WriteLine(a.AllEqual(b, c));
                Console.ReadLine();
            } 
        }

        static class Extensions
    {
        static public  bool AllEqual(this bool firstValue, params bool[] bools)
        {
            return bools.All(thisBool => thisBool == firstValue);
        }
    }
1
  • you're right. And the answer is really great. But in right now it's not likely this will be necessary.
    – superM
    Commented Jun 29, 2012 at 19:20
4

how about using this?

areSame = (a == b && b == c);
1

What about:

bool areSame = (a && b && c) || (!a && !b && !c) ? true : false;
3
  • This smth like if(true == true), which is tautology.
    – superM
    Commented Jun 29, 2012 at 14:38
  • It might be a little verbose (I'm not sure of C# syntax), but it's not tautological — it's if (true) then true else false, not if (true == true) Commented Jun 29, 2012 at 20:43
  • @anotherdave you are both correct. It is verbose and tautological. The ternary conditional vs output can be understood as identical. In C#, conditionals must always be boolean so bool y = x ? true : false; is the same as var y = (bool)x and bool y = x; thus bool areSame = (a && b && c) || (!a && !b && !c) ? true : false; is the same as bool areSame = (a && b && c) || (!a && !b && !c); in every possible interpretation (even considering operator overloading, short-circuiting and implicit type conversions). a == b == c; is the less verbose and easier to understand alternative. Commented Jul 11, 2018 at 23:55
0

What about this approach? It will allow you to pass in as many bools and see if the min number of trues are met.

public static bool AreMinNumberOfTruesMet(int minNumOftrue, params bool[] bools)
{
    var totalTrue = bools.Count(boolean => boolean == true);

    return totalTrue >= minNumOftrue;
}
0

Or in one line :

bool allSame = !bools.Any(b => b != bools[0])
-1
//all same
bool allSame = a == b == c == d;

//all false
bool allFalse = a == b == c == d == false;
2
  • 1
    Although this code might solve the problem, a good answer should also contain an explanation.
    – BDL
    Commented Jan 30, 2017 at 7:58
  • This answer is incorrect. This assertion fails: bool allSame = true == false == false == true; Debug.Assert(!allSame); The four booleans true, false, false and true are not the same, so the assertion !allSame should succeed. Commented Aug 30, 2021 at 23:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.