Skip to content

Instantly share code, notes, and snippets.

@DForshner
Created June 4, 2014 21:39
Show Gist options
  • Select an option

  • Save DForshner/5c14553bf70b00fc6c82 to your computer and use it in GitHub Desktop.

Select an option

Save DForshner/5c14553bf70b00fc6c82 to your computer and use it in GitHub Desktop.
Returns true if there are any true values in an array.
public static class BoolExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[DebuggerStepThrough]
public static bool AnyTrue(this IEnumerable<bool> bools)
{
return bools.Aggregate((a, b) => (a || b));
}
}
[TestClass]
public class BoolExtensionsTests
{
[TestMethod]
public void AnyTrue_WhenNoTrue_ExpectFalse()
{
var bools = new bool[] { false, false, false, false, false };
Assert.IsFalse(bools.AnyTrue());
}
[TestMethod]
public void AnyTrue_WhenLastTrue_ExpectTrue()
{
var bools = new bool[] { false, false, false, false, true };
Assert.IsTrue(bools.AnyTrue());
}
[TestMethod]
public void AnyTrue_WhenFirstTrue_ExpectTrue()
{
var bools = new bool[] { false, false, false, false, true };
Assert.IsTrue(bools.AnyTrue());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment