Contains Extension MethodUse the Contains extension method to search for a matching element.
This page was last reviewed on Dec 12, 2021.
Contains. This C# method indicates whether the IEnumerable collection has an element matching the argument. It acts upon any IEnumerable collection when the System.Linq namespace is included.
Bool Method
C# method notes. Contains() is an extension method. Some collections, such as List, offer their own Contains method—this is an instance method.
List Contains
This program tests the Contains extension method, and the List method, on the same List. The result is the same for both Contains calls. The element with value 7 is located within the List.
Info In this example, the first Contains call specifies the type parameter int in the angle brackets.
And This tells the C# compiler to select the extension method version of Contains, not the List type's version.
using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Use extension method. bool a = list.Contains<int>(7); // Use instance method. bool b = list.Contains(7); Console.WriteLine(a); Console.WriteLine(b); } }
True True
Performance. The different Contains methods are implemented with different code. The version specific to List is faster. I tested the same List with the 2 Contains methods.
Version 1 The extension method Contains() from System.Linq is used to search the List.
Version 2 List Contains() is used. The exception code is used to ensure the C# compiler does not optimize out the Contains calls.
Result The Contains method (on List) is faster than the Contains extension. It thus should be preferred.
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; class Program { const int _max = 100000; static void Main() { var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var s1 = Stopwatch.StartNew(); // Version 1: Contains extension. for (int i = 0; i < _max; i++) { bool a = list.Contains<int>(7); if (!a) { throw new Exception(); } } s1.Stop(); var s2 = Stopwatch.StartNew(); // Version 2: List contains. for (int i = 0; i < _max; i++) { bool b = list.Contains(7); if (!b) { throw new Exception(); } } s2.Stop(); Console.WriteLine(s1.Elapsed.TotalMilliseconds); Console.WriteLine(s2.Elapsed.TotalMilliseconds); } }
4.3162 (ms for Contains extension) 3.4375 (ms for List Contains
Discussion. Internally, the Contains extension method checks for errors. It then uses the IEnumerable type's enumerator implementation to loop through the elements.
Info In a List, the enumerator searches forward. The MoveNext method is used.
So The performance disparity here is mainly due to more low-level code in List Contains.
And List Contains internally does not use enumerators or MoveNext—it uses a loop construct and checks elements. This is lower-level.
List Contains
A summary. The Contains extension method can be called on any type that implements IEnumerable. It has no relation to the List Contains method, except the name and concept.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 12, 2021 (new example).
© 2007-2023 Sam Allen.