Intersect one HashSet to another HashSet in CSharp

Description

The following code shows how to intersect one HashSet to another HashSet.

Example


using System;//w ww  .  j ava  2 s.c o m
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        HashSet<int> evenNumbers = new HashSet<int>();
        HashSet<int> oddNumbers = new HashSet<int>();

        for (int i = 0; i < 5; i++)
        {
            evenNumbers.Add(i * 2);
            oddNumbers.Add((i * 2) + 1);
        }
        HashSet<int> numbers = new HashSet<int>(evenNumbers);
        numbers.IntersectWith(oddNumbers);

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




















Home »
  C# Tutorial »
    Collections »




ArrayList
BitArray
Collection
Comparer
HashSet
Hashtable
LinkedList
List
ListDictionary
OrderedDictionary
Queue
SortedList
SortedSet
Stack
StringCollection
StringDictionary