Remove all elements in a collection from a HashSet in CSharp

Description

The following code shows how to remove all elements in a collection from a HashSet.

Example


using System;//from   w  w w. j  a  v  a  2  s  .  co  m
using System.Collections.Generic;

public class MainClass
{
    public static void Main(String[] argv)
    {
        HashSet<int> lowNumbers = new HashSet<int>();
        HashSet<int> highNumbers = new HashSet<int>();

        for (int i = 0; i < 6; i++)
        {
            lowNumbers.Add(i);
        }

        for (int i = 3; i < 10; i++)
        {
            highNumbers.Add(i);
        }

        foreach(int i in lowNumbers){
           Console.WriteLine(i);
        }
        foreach(int i in highNumbers){
           Console.WriteLine(i);
        }

        highNumbers.ExceptWith(lowNumbers);

        foreach(int i in highNumbers){
           Console.WriteLine(i);
        }

    }
}

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