Set operator intersect

In this chapter you will learn:

  1. Intersect with another collection

Intersect with another collection

IntersectsWith keep only elements that are present in that object and in the specified collection.

We keep all the vowels from our set of characters in the following code:

using System;//from java  2s . c om
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class Sample
{
    public static void Main()
    {
        var letters = new HashSet<char>("the quick brown fox");
        letters.IntersectWith("aeiou");
        foreach (char c in letters)
            Console.Write(c);
    }

}

The output:

Next chapter...

What you will learn in the next chapter:

  1. How to remove elements from HashSet with condition
Home » C# Tutorial » Collections
HashSet
Set operation: Except
Set operator intersect
Remove with condition
HashSet contains
HashSet creation
HashSet removing element
Sub set operations
Hashtable
Hashtable ContainsValue
Add elements to a Hashtable
Hashtable containsKey
Remove from Hashtable
Hashtable to array
Queue
Generic Queue
Stack