Introduction: C# SortedSet

In this post, we will learn about C# SortedSet with an example.C# SortedSet class is used to store, remove or view elements from collection.It maintains ascending order and store only unique elements. It is found in System.Collections.Generic namespace.

SortedSet does not include hashing, meaning that it has to do linear searches for lookups. Therefore, the SortedSet is slower than the HashSet for most cases where you need to do lookups.


Description:

In my previous posts I have explained various topics like Collections in C#,Stack in C#,Queue in C#.Net,LinkedList in C#.Net. Now in this post, I will explain about C# SortedSet with appropriate example.


Methods of SortedSet

1. Count:

Gets the number of elements in the SortedSet.

2. Max:

Gets the maximum value in the SortedSet

3. Min:

Gets the minimum value in the SortedSet

4. Clear:

Removes all elements from the set.

Now, Create Console Application in Visual Studio and write below line of code.

using System;
using System.Collections.Generic;

namespace ConsoleDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a set of strings in SortedSet
            var _SiteList = new SortedSet<string>();
            _SiteList.Add("DotNetK.com");
            _SiteList.Add("Live24u.com");
            _SiteList.Add("w3school.com");
            _SiteList.Add("DotNetK.com");//will not be added because of duplicate

            // Iterate SortedSet elements using foreach loop and print on console  
            foreach (var site in _SiteList)
            {
                Console.WriteLine(site);
            }

            Console.Read();
        }
    }   
}
C# SortedSet

C# SortedSet

I hope you got an idea about C# SortedSet.
I would like to have feedback from my blog readers.
Your valuable feedback, question, or comments about this article are always welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website