C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Var: A string List is constructed. Three string literal references are added to the collection's internal arrays through the Add method.
VarString LiteralList AddOrdinal: StringComparer.OrdinalIgnoreCase implements the IEqualityComparer interface. This allows an insensitive string search.
IEqualityComparerReturns: Contains returns a bool value type. We can store its result in a bool. We can use the Contains method as an expression.
BoolC# program that uses List Contains method
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Create List with three elements.
var list = new List<string>();
list.Add("cat");
list.Add("dog");
list.Add("moth");
// Search for this element.
if (list.Contains("dog"))
{
Console.WriteLine("dog was found");
}
// 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)");
}
// This element is not found.
Console.WriteLine(list.Contains("fish"));
}
}
Output
dog was found
MOTH was found (insensitive)
False
Version 1: This version of the code uses a for-loop to test each element in the List, exiting early if a match is found.
Version 2: This version uses the Contains method on the List, which reduces the amount of code needed.
Result: A custom for-loop was faster. Typically writing a for-loop that does a search is faster than using a built-in method.
C# program that benchmarks List Contains
using System;
using System.Collections.Generic;
using System.Diagnostics;
class Program
{
/// <summary>
/// Custom implementation.
/// </summary>
static bool ContainsLoop(List<string> list, string value)
{
for (int i = 0; i < list.Count; i++)
{
if (list[i] == value)
{
return true;
}
}
return false;
}
const int _max = 100000000;
static void Main()
{
List<string> list = new List<string>();
list.Add("one");
list.Add("two");
list.Add("three");
list.Add("four");
list.Add("five");
var s1 = Stopwatch.StartNew();
// Version 1: use loop to search a list.
for (int i = 0; i < _max; i++)
{
bool f = ContainsLoop(list, "four");
}
s1.Stop();
var s2 = Stopwatch.StartNew();
// Version 2: use Contains method.
for (int i = 0; i < _max; i++)
{
bool f = list.Contains("four");
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.Read();
}
}
Output
19.22 ns For-loop, string comparisons [ContainsLoop]
54.60 ns Contains method [Contains]
Warning: LINQ extension methods are often slower. They operate on the IEnumerable interface.
IEnumerableParameters: The LINQ Contains method accepts 2 parameters (it accepts an IEqualityComparer).
Info: This is often much slower than a Dictionary. The LINQ method also does a linear search.