Boolean list

  • Thread starter Thread starter Luigi
  • Start date Start date
L

Luigi

Hi all,
having a List<bool> (C# 2.0) how can I extract a boolean value that is true
if *all* value in the list is true, and false if almost one of them is false?

Thanks in advance.
 
using System.Linq;
(...)
!aList.Any(b => !b)

Check the edge-case with the empty set.

Regards

--http://www.pajacyk.pl

public List<Boolean> GetValueFromList(List<Boolean> SetOfValue,Boolean
Value)
{

List<Boolean> Result = new List<Boolean>;

foreach (Boolean B in SetOfValue)
{
if (B)
{
Result.Add(B);
}
}

return Result;
}
 
In my idea then you can better use a byte and check that with normal boolean
or, and and xor.

However a little bit from the time when a cycle cost 1 second.

Cor
 
public List<Boolean> GetValueFromList(List<Boolean> SetOfValue,Boolean
Value)
{

    List<Boolean> Result = new List<Boolean>;

      foreach (Boolean B in SetOfValue)
       {
            if (B)
              {
                  Result.Add(B);
              }
       }

      return Result;

}

in addition :

Boolean Check(List<Boolean> Value)
{
Boolean Result = True;

foreach (Boolean B in Value)
{
if (!B)
{
Result = False;
break;
}

return Result;
}
}
 
maciek kanski said:
using System.Linq;
(...)
!aList.Any(b => !b)

Check the edge-case with the empty set.

Unfortunately I can't use LINQ because I'm "fixed" with 2.0 framework.

Luigi
 
Hi all,
having a List<bool> (C# 2.0) how can I extract a boolean value that is true
if *all* value in the list is true, and false if almost one of them is false?

Thanks in advance.

You can use this line:

bool allTrue = listofbools.TrueForAll(delegate(bool value)
{ return value; });

Chris
 
having a List said:
true
if *all* value in the list is true, and false if almost one of them is
false?

bool AllTrue(List<bool> list)
{
foreach (bool b in list)
if (!b)
return false;
return true;
}

Oleg Subachev
 
in addition :

Boolean Check(List<Boolean> Value)
{
Boolean Result = True;

foreach (Boolean B in Value)
{
if (!B)
{
Result = False;
break;
}

return Result;
}
}

I hope that yields some compiler warnings. In addition to some very
misguided attempt to maintain a single exit point and multiple misspelled
keywords, and using keywords as identifiers by varying only case, you've put
the unconditional (single exits are always unconditional) exit point inside
the loop.

Much better:

bool AreAllTrue(List<bool> booleans)
{
foreach (bool element in booleans)
{
if (!element) return false;
}
return true;
}
 
Luigi said:
Hi all,
having a List<bool> (C# 2.0) how can I extract a boolean value that is
true
if *all* value in the list is true, and false if almost one of them is
false?

Thanks in advance.

Really simple answer:

!booleans.Contains(false)
 
Back
Top