Sorted Set


using System;
using System.Collections.Generic;

// SortedSet adds elements in sorted order irrespective of the order in which we add them.
class SortedSetDemo
{
    static void Main(string[] args)
    {
        SortedSet<int> sortedset = new SortedSet<int>();  // Declaring a SortedSet

        // Adding values to SortedSet
        sortedset.Add(10);
        sortedset.Add(6);
        sortedset.Add(8);
        sortedset.Add(7);

        // For displaying values in a SortedSet,we can use a foreach loop
        int limit = sortedset.Count;   // Gets the size of SortedSet
        Console.WriteLine("Size of SortedSet: " + limit);
        Console.WriteLine("Using foreach loop");
        foreach (int val in sortedset// Displays values in alphabetical order.
        {
            Console.WriteLine(val);
        }

        Console.ReadLine();

    }
}

Output:
Size of SortedSet: 4
Using foreach loop
6
7
8
10