Most efficient way to find if a value exists within a C# List

Just use bool trueInList = list.Contains(true);. This loops the list until there's a true.

Why do you need something faster with such a simple use-case?


Use either list.Contains(true) or list.Any(true). For a normal list both have complexity O(n). Since Any() is an extension method though, which needs to invoke delegates, the Contains() might still be a bit faster. But to be sure I would simply test both with a large collection.


You could use Any().

list.Any(b => b);

Tags:

C#

List