adding booleans (as integer) [duplicate]

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();

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)