Set operation: Except

In this chapter you will learn:

  1. How to do Set operation Except
  2. Set opertation SymmetricExceptWith

Set operation Except

ExceptWith removes the specified elements from the source set. Here, we strip all vowels from the set:

using System;/*from j  a  va2  s  . c o m*/
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.ExceptWith("aeiou");
        foreach (char c in letters)
            Console.Write(c);
    }

}

The output:

SymmetricExceptWith

SymmetricExceptWith removes all but the elements that are unique to one set or the other.

using System;//  ja v  a  2 s . 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.SymmetricExceptWith("the lazy brown fox");
        foreach (char c in letters)
            Console.Write(c);
    }

}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Intersect with another collection
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