A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/c-sharp/c-sharp-intersection-of-sortedset-with-a-collection/ below:

C# | Intersection of SortedSet with a collection

C# | Intersection of SortedSet with a collection

Last Updated : 11 Jul, 2025

SortedSet

class represents the collection of objects in sorted order. This class comes under the

System.Collections.Generic

namespace.

SortedSet<T>.IntersectWith(IEnumerable<T>)

method is used to modify the current SortedSet<T> object so that it contains only elements that are also in a specified collection.

Properties: Syntax:
mySortedSet1.IntersectWith(mySortedSet2);

Here,

mySortedSet1

and

mySortedSet2

are two SortedSet's object.

Exception:

This method will give

ArgumentNullException

if the SortedSet is

null

. Below given are some examples to understand the implementation in a better way:

Example 1: CSHARP
// C# code to get the Intersection of 2 SortedSets
using System;
using System.Collections.Generic;

class GFG {

    // Driver code
    public static void Main()
    {

        // Creating a SortedSet of integers
        SortedSet<int> mySortedSet1 = new SortedSet<int>();

        // adding elements in mySortedSet1
        mySortedSet1.Add(2);
        mySortedSet1.Add(4);
        mySortedSet1.Add(6);
        mySortedSet1.Add(8);
        mySortedSet1.Add(10);

        // Creating a SortedSet of integers
        SortedSet<int> mySortedSet2 = new SortedSet<int>();

        // adding elements in mySortedSet
        mySortedSet2.Add(4);
        mySortedSet2.Add(5);
        mySortedSet2.Add(7);
        mySortedSet2.Add(8);
        mySortedSet2.Add(9);

        Console.WriteLine("The intersection of mySortedSet1 and mySortedSet2:");

        mySortedSet1.IntersectWith(mySortedSet2);

        // To display the intersection 
        // of mySortedSet1 and mySortedSet2
        foreach(int i in mySortedSet1)
        {
            Console.WriteLine(i);
        }
    }
}
Output:
The intersection of mySortedSet1 and mySortedSet2: 
4
8
Example 2: CSHARP
// C# code to get the Intersection of 2 SortedSets
using System;
using System.Collections.Generic;

class GFG {

    // Driver code
    public static void Main()
    {

        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet1 = new SortedSet<string>();

        // adding elements in mySortedSet1
        mySortedSet1.Add("Geeks");
        mySortedSet1.Add("for");
        mySortedSet1.Add("Geeks");
        mySortedSet1.Add("Noida");
        mySortedSet1.Add("Data Structures");

        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet2 = new SortedSet<string>();

        // adding elements in mySortedSet
        mySortedSet2.Add("Geeks");
        mySortedSet2.Add("Java");
        mySortedSet2.Add("Geeks Classes");
        mySortedSet2.Add("C++");
        mySortedSet2.Add("Noida");

        Console.WriteLine("The Intersection of mySortedSet1 and mySortedSet2:");

        mySortedSet1.IntersectWith(mySortedSet2);

        // To display the intersection of
        // mySortedSet1 and mySortedSet2
        foreach(string str in mySortedSet1)
        {
            Console.WriteLine(str);
        }
    }
}
Output:
The Intersection of mySortedSet1 and mySortedSet2 is: 
Geeks
Noida
Reference:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4