Check if a HashSet contains the specified element in CSharp

Description

The following code shows how to check if a HashSet contains the specified element.

Example


using System;/*from w w  w . j  av  a2s.  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);
    }

    Console.WriteLine(evenNumbers.Contains(0));
    if (evenNumbers.Contains(0))
    {
       evenNumbers.Remove(0);
    }
    Console.WriteLine(evenNumbers.Contains(0));
    
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary