C# HashSet RemoveWhere

Description

HashSet RemoveWhere removes all elements that match the conditions defined by the specified predicate from a HashSet collection.

Syntax

HashSet.RemoveWhere has the following syntax.


public int RemoveWhere(
  Predicate<T> match
)

Parameters

HashSet.RemoveWhere has the following parameters.

  • match - The Predicate delegate that defines the conditions of the elements to remove.

Returns

HashSet.RemoveWhere method returns The number of elements that were removed from the HashSet collection.

Example

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


/*w w w .  ja  va  2  s.co m*/
using System;
using System.Collections.Generic;
public class MainClass{
  public static void Main(String[] argv){  
        HashSet<int> evenNumbers = new HashSet<int>();

        for (int i = 0; i < 20; i++)
        {
            evenNumbers.Add(i);
        }
        evenNumbers.RemoveWhere(isEven);

        Console.Write("evenNumbers contains {0} elements: ", evenNumbers.Count);

  }
    private static bool isEven(int i)
    {
        return ((i % 2) == 1);
    }

}
    

The code above generates the following result.





















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




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack