HashSet<T>.Remove Method (T)
.NET Framework (current version)
Removes the specified element from a HashSet<T> object.
Assembly: System.Core (in System.Core.dll)
Parameters
- item
-
Type:
T
The element to remove.
Return Value
Type: System.Booleantrue if the element is successfully found and removed; otherwise, false. This method returns false if item is not found in the HashSet<T> object.
Implements
ICollection<T>.Remove(T)If the HashSet<T> object does not contain the specified element, the object remains unchanged. No exception is thrown.
This method is an O(1) operation.
The following example demonstrates how to remove values from a HashSet<T> collection using the Remove method. In this example, zero is arbitrarily removed from the HashSet<T> collection.
using System; using System.Collections.Generic; class Example { static void Main() { HashSet<int> numbers = new HashSet<int>(); for (int i = 0; i < 20; i++) { numbers.Add(i); } // Display all the numbers in the hash table. Console.Write("numbers contains {0} elements: ", numbers.Count); DisplaySet(numbers); // Remove all odd numbers. numbers.RemoveWhere(IsOdd); Console.Write("numbers contains {0} elements: ", numbers.Count); DisplaySet(numbers); // Check if the hash table contains 0 and, if so, remove it. if (numbers.Contains(0)) { numbers.Remove(0); } Console.Write("numbers contains {0} elements: ", numbers.Count); DisplaySet(numbers); } private static bool IsOdd(int i) { return ((i % 2) == 1); } private static void DisplaySet(HashSet<int> set) { Console.Write("{"); foreach (int i in set) Console.Write(" {0}", i); Console.WriteLine(" }"); } } // This example display the following output: // numbers contains 20 elements: { 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 } // numbers contains 10 elements: { 0 2 4 6 8 10 12 14 16 18 } // numbers contains 9 elements: { 2 4 6 8 10 12 14 16 18 }
Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Show: