33

I wrote a code in AS3 which allowed me to check if a particular number of things were true...

If (true + false + true + true + false + true + true < 4)
{

}

When i tried rewriting in C#, it tells me i cannot add type bool and bool. Is the best way of doing this to rewrite it like this? Or is there some simpler work around?

If ((true?1:0) + (false?1:0) + (true?1:0) + (true?1:0) + (false?1:0) + (true?1:0) + (true?1:0) < 4)
{

}
Mario S
  • 11,715
  • 24
  • 39
  • 47
Joel
  • 1,580
  • 4
  • 20
  • 33

6 Answers6

66

Try using IEnumerable<T>.Count(Func<T,bool>) from System.Linq, with T as bool, on a params method parameter.

public static int CountTrue(params bool[] args)
{
   return args.Count(t => t);
}

Usage

// The count will be 3
int count = CountTrue(false, true, false, true, true);

You can also introduce a this extension method:

public static int TrueCount(this bool[] array)
{
   return array.Count(t => t);
}

Usage

// The count will be 3
int count = new bool[] { false, true, false, true, true }.TrueCount();
Joel Purra
  • 24,294
  • 8
  • 60
  • 60
17

You could create an array and use Count:

if ((new []{true, false, true, true, false, true, true}).Count(x=>x) < 4)
{

}

or the Sum method:

if ((new []{true, false, true, true, false, true, true}).Sum(x=>x?1:0) < 4)
{

}
sloth
  • 99,095
  • 21
  • 171
  • 219
  • If i were using it as a one off, this is how i'd do it, thanks heaps for replying *upvotes* – Joel Sep 07 '12 at 11:36
  • 1
    Thanks! This was my favorite approach. I had a few bools I needed to add like this, and (new[] {var1, var2, var3, var4}).Sum(x => x ? 1 : 0); worked great for me! – Stachu Oct 02 '13 at 15:32
13

Here's a more fun example:

if ((BoolCount)true + false + true + true + false + true + true <= 5)
{
    Console.WriteLine("yay");
}

Using this class:

struct BoolCount
{
    private readonly int c;
    private BoolCount(int c) { this.c = c; }

    public static implicit operator BoolCount(bool b)
        { return new BoolCount(Convert.ToInt32(b)); }

    public static implicit operator int(BoolCount me)
        { return me.c; }

    public static BoolCount operator +(BoolCount me, BoolCount other)
        { return new BoolCount(me.c + other.c); }
}
porges
  • 30,133
  • 4
  • 83
  • 114
5

Convert.ToInt32(true) + Convert.ToInt32(false) + Convert.ToInt32(true) also work in this case i think this is simplest way we have

0

You could write an extension method for the BitArray class. I'm not sure if the performance would be any better than using Linq, but at least its another option:

public static int CountSetBits(this BitArray theBitArray)
{
   int count = 0;
   for (int i = 0; i < theBitArray.Count; i++)
   {
      count += (theBitArray.Get(i)) ? 1 : 0;
   }
   return count; 
}

Usage:

BitArray barray = new BitArray(new [] { true, true, false, true });
int count = barray.CountSetBits(); // Will return 3
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
-1

(Inspired by L.B.:s comment) you could write an extension method:

static class Ex
{
    public static int Int(this bool x) { return x ? 1 : 0; }
}

And then (provided you include a using to the namespace containing the Ex class), you can write your if statement like this:

if (true.Int() + false.Int() + true.Int() + true.Int() + 
    false.Int() + true.Int() + true.Int() < 4)
{
    ...
}
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114