Export (0) Print
Expand All

HashSet<T>.RemoveWhere Method

Removes all elements that match the conditions defined by the specified predicate from a HashSet<T> collection.

Namespace:  System.Collections.Generic
Assembly:  System.Core (in System.Core.dll)

public int RemoveWhere(
	Predicate<T> match
)

Parameters

match
Type: System.Predicate<T>

The Predicate<T> delegate that defines the conditions of the elements to remove.

Return Value

Type: System.Int32
The number of elements that were removed from the HashSet<T> collection.

ExceptionCondition
ArgumentNullException

match is null.

Calling this method is an O(n) operation, where n is Count.

The following example demonstrates how to remove values from a HashSet<T> collection using the Remove method. In this example, all odd integers are removed from the HashSet<T> collection as specified by the match delegate.

    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 }
 */

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft