Open In App
Related Articles

C# | Check if the SortedSet contains a specific element

Improve Article
Improve
Save Article
Save
Like Article
Like

SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Contains(T) Method is used to check if a SortedSet contains a specific element or not.

Properties:

  • In C#, SortedSet class can be used to store, remove or view elements.
  • It maintains ascending order and does not store duplicate elements.
  • It is suggested to use SortedSet class if you have to store unique elements and maintain ascending order.

Syntax:

public virtual bool Contains (T item);

Here, T is Type Parameter, i.e, the type of elements in the set.

Return Value: The method returns True if the set contains the specified item, otherwise, returns False.

Example 1:




// C# code to check if the SortedSet
// contains a specific element
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet = new SortedSet<string>();
  
        // adding elements in mySortedSet
        mySortedSet.Add("Mumbai");
        mySortedSet.Add("Bangalore");
        mySortedSet.Add("Chandigarh");
        mySortedSet.Add("Noida");
        mySortedSet.Add("Jaipur");
        mySortedSet.Add("Kolkata");
  
        // Checking if the SortedSet contains
        // a specific element
        Console.WriteLine(mySortedSet.Contains("Delhi"));
    }
}


Output:

True

Example 2:




// C# code to check if the SortedSet
// contains a specific element
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet<string> mySortedSet = new SortedSet<string>();
  
        // adding elements in mySortedSet
        mySortedSet.Add("Mumbai");
        mySortedSet.Add("Bangalore");
        mySortedSet.Add("Chandigarh");
        mySortedSet.Add("Noida");
        mySortedSet.Add("Jaipur");
        mySortedSet.Add("Kolkata");
  
        // Checking if the SortedSet contains
        // a specific element
        Console.WriteLine(mySortedSet.Contains("Delhi"));
    }
}


Output:

False

Reference:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 Feb, 2019
Like Article
Save Article
Previous
Next
Similar Reads