Open In App

C# | Get the maximum value in the SortedSet

SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. SortedSet<T>.Max Property is used to get the maximum value in the SortedSet which is defined by the comparer.

Properties:



Syntax :

mySet.Max

Here, mySet is a SortedSet.



Return Value: The maximum value in the SortedSet.

Example 1:




// C# code to get the maximum value
// in the SortedSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of integers
        SortedSet<int> mySet = new SortedSet<int>();
  
        // Inserting elements into SortedSet
        for (int i = 0; i < 10; i++) {
            mySet.Add(i);
        }
  
        // Displaying the maximum value in the SortedSet
        Console.WriteLine("The maximum element in SortedSet is : " + mySet.Max);
    }
}

Output:
The maximum element in SortedSet is : 9

Example 2:




// C# code to get the maximum value
// in the SortedSet
using System;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a SortedSet of strings
        SortedSet<string> mySet = new SortedSet<string>();
  
        // Inserting elements into SortedSet
        mySet.Add("A");
        mySet.Add("B");
        mySet.Add("C");
        mySet.Add("D");
        mySet.Add("E");
        mySet.Add("F");
  
        // Displaying the maximum value in the SortedSet
        Console.WriteLine("The maximum element in SortedSet is : " + mySet.Max);
    }
}

Output:
The maximum element in SortedSet is : F

Reference:


Article Tags :
C#