Mid Collections

How do you perform operations like union, intersection, and difference with SortedSet<T>?

Operation Method Description

Union UnionWith() Adds all elements from another set

Intersection IntersectWit

h()

Keeps only elements present in both

sets

Difference ExceptWith() Removes elements found in another set

Example:

SortedSet<int> set1 = new SortedSet<int> { 1, 2, 3 };

SortedSet<int> set2 = new SortedSet<int> { 3, 4, 5 };

set1.UnionWith(set2); // {1, 2, 3, 4, 5}

set1.IntersectWith(set2); // {3, 4, 5} (if applied on the original

set1)

set1.ExceptWith(set2); // {1, 2} (if applied on the original

set1)

More from C# Programming Tutorial

All questions for this course