C# HashSet IntersectWith
Description
HashSet
modifies the current
HashSet
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 HashSetobject.
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);
}
}
}