C# HashSet Contains

Description

HashSet Contains determines whether a HashSet object contains the specified element.

Syntax

HashSet.Contains has the following syntax.


public bool Contains(
  T item
)

Parameters

HashSet.Contains has the following parameters.

  • item - The element to locate in the HashSet object.

Returns

HashSet.Contains method returns true if the HashSet object contains the specified element; otherwise, false.

Example

The following example demonstrates how to remove values from a HashSet collection using the Remove method.


using System;/*from w w  w. j  ava 2 s .  c o m*/
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
    HashSet<int> evenNumbers = new HashSet<int>();
    for (int i = 0; i < 5; i++){
        evenNumbers.Add(i * 2);
    }
    if (evenNumbers.Contains(0))
    {
       evenNumbers.Remove(0);
    }
  }
}




















Home »
  C# Tutorial »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack