C# HashSet Remove

Description

HashSet Remove removes the specified element from a HashSet object.

Syntax

HashSet.Remove has the following syntax.


public bool Remove(
  T item
)

Parameters

HashSet.Remove has the following parameters.

  • item - The element to remove.

Returns

HashSet.Remove method returns true if the element is successfully found and removed; otherwise, false. This method returns false if item is not found in the HashSet object.

Example

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


using System;/*from   w  w w .  j a va2  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