c# check all element in a list are true code example
Example: c# combine list of bool
var myList = new List<bool>();
for (int x = 0; x < 10000000; x++)
myList.Add(false);
var containsAllFalse = false;
Stopwatch sw = new Stopwatch();
sw.Start();
containsAllFalse = !myList.Any(x => x);
sw.Stop();
var timeAny = sw.ElapsedMilliseconds;
containsAllFalse = false;
sw.Restart();
containsAllFalse = myList.All(x => x == false);
sw.Stop();
var timeAll = sw.ElapsedMilliseconds;
containsAllFalse = false;
sw.Restart();
containsAllFalse = !myList.Exists(x => x == true);
sw.Stop();
var timeExists = sw.ElapsedMilliseconds;
containsAllFalse = false;
sw.Restart();
containsAllFalse = !myList.Contains(true);
sw.Stop();
var timeContains = sw.ElapsedMilliseconds;
var percentFaster = Math.Round((double)timeAny / timeContains, 2);
Console.WriteLine("Elapsed via Any = {0}ms", timeAny);
Console.WriteLine("Elapsed via All = {0}ms", timeAll);
Console.WriteLine("Elapsed via Exists = {0}ms", timeExists);
Console.WriteLine("Elapsed via Contains = {0}ms", timeContains);
Console.WriteLine("Contains is ~{0}x faster than Any!", percentFaster);