Part 4 Contains returns a bool. When an element is not found, Contains returns false. We can store the result in an expression.
// Part 1: create List with three elements.var list = new List<string>();
// Part 2: search for this element.
Console.WriteLine("dog was found");
// Part 3: search for this element in any string case.// ... This is the LINQ method with the same name.
if (list.Contains("MOTH", StringComparer.OrdinalIgnoreCase))
Console.WriteLine("MOTH was found (insensitive)");
// Part 4: this element is not found.
Console.WriteLine(list.Contains("fish"));dog was found
MOTH was found (insensitive)
Benchmark. We benchmark the performance of List Contains and a custom for-loop on a small List. Only a few elements are searched by each method.