HashSet<T>.Contains Method
Determines whether a HashSet<T> object contains the specified element.
Assembly: System.Core (in System.Core.dll)
Parameters
- item
- Type: T
The element to locate in the HashSet<T> object.
Return Value
Type: System.Booleantrue if the HashSet<T> object contains the specified element; otherwise, false.
Implements
ICollection<T>.Contains(T)The following example demonstrates how to remove values from a HashSet<T> collection using the Remove method. In this example, the Contains method verifies that the set contains a value before removing it.
static void Main()
{
HashSet<int> evenNumbers = new HashSet<int>();
for (int i = 0; i < 20; i++)
{
evenNumbers.Add(i);
}
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
DisplaySet(evenNumbers);
evenNumbers.RemoveWhere(isEven);
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
DisplaySet(evenNumbers);
if (evenNumbers.Contains(0))
{
evenNumbers.Remove(0);
}
Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);
DisplaySet(evenNumbers);
}
private static bool isEven(int i)
{
return ((i % 2) == 1);
}
/* This example produces output similar to the following:
* evenNumbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 }
* evenNumbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 }
* evenNumbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }
*/
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.