C# HashSet IntersectWith

Description

HashSet IntersectWith modifies the current HashSet object to contain only elements that are present in that object and in the specified collection.

Syntax

HashSet.IntersectWith has the following syntax.


public void IntersectWith(
  IEnumerable<T> other
)

Parameters

HashSet.IntersectWith has the following parameters.

  • other - The collection to compare to the current HashSet object.

Returns

HashSet.IntersectWith method returns

Example


using System;/*from w  w w.jav a2s .  co 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 »
    System.Collections.Generic »




HashSet
LinkedList
LinkedListNode
List
Queue
SortedSet
Stack