unigraphique.com

Understanding SortedSet in C#: A Comprehensive Guide

Written on

Chapter 1: Introduction to SortedSet

SortedSet belongs to the System.Collections.Generic namespace and is designed to store elements in a sorted manner. This collection type is particularly useful when there is a need to maintain order among the elements.

In scenarios where sorted elements are required, SortedSet proves to be an ideal choice.

Creating a SortedSet

To create a SortedSet, you will need to utilize the System.Collections.Generic namespace. Below is an example of how to set up SortedSets for fruit prices and fruit names:

using System;

using System.Collections.Generic;

SortedSet<int> fruitPrices = new SortedSet<int>();

SortedSet<string> fruitNames = new SortedSet<string>();

Adding Elements

Elements can be added to a SortedSet using the Add method. Here’s how to add some fruit prices and names:

fruitPrices.Add(10);

fruitPrices.Add(18);

fruitPrices.Add(34);

fruitNames.Add("Apple");

fruitNames.Add("Banana");

fruitNames.Add("Cherry");

Accessing Elements

You can access the elements within a SortedSet using a foreach loop. Here’s an example of how to iterate through the fruit prices:

foreach (int price in fruitPrices)

{

Console.WriteLine(price);

}

bool contains = fruitPrices.Contains(18); // Returns true

foreach (string name in fruitNames)

{

Console.WriteLine(name);

}

bool containsName = fruitNames.Contains("Apple"); // Returns true

Removing Elements

To remove elements from a SortedSet, the Remove method can be employed. Here’s a quick example:

fruitPrices.Remove(2);

fruitNames.Remove("Banana");

Additional Operations with SortedSet

SortedSet offers a variety of methods and properties for performing operations like element searches, checking for existence, and clearing the set:

int countPrices = fruitPrices.Count;

int countNames = fruitNames.Count;

fruitPrices.Clear(); // Empties the set

fruitNames.Clear(); // Empties the set

Set Operations: Union, Intersection, and Difference

SortedSet also supports mathematical set operations such as union, intersection, and difference. Here’s an example:

SortedSet<int> set1 = new SortedSet<int> { 1, 2, 3, 4 };

SortedSet<int> set2 = new SortedSet<int> { 3, 4, 5, 6 };

SortedSet<int> unionSet = new SortedSet<int>(set1);

unionSet.UnionWith(set2); // {1, 2, 3, 4, 5, 6}

SortedSet<int> intersectionSet = new SortedSet<int>(set1);

intersectionSet.IntersectWith(set2); // {3, 4}

SortedSet<int> differenceSet = new SortedSet<int>(set1);

differenceSet.ExceptWith(set2); // {1, 2}

Complete Example of SortedSet in C#

Here’s a complete program demonstrating the usage of SortedSet:

using System;

using System.Collections.Generic;

public class Program

{

public static void Main(string[] args)

{

SortedSet<int> fruitPrices = new SortedSet<int>();

SortedSet<string> fruitNames = new SortedSet<string>();

fruitPrices.Add(10);

fruitPrices.Add(18);

fruitPrices.Add(34);

fruitNames.Add("Apple");

fruitNames.Add("Banana");

fruitNames.Add("Cherry");

foreach (int price in fruitPrices)

{

Console.Write(price + " ");

}

Console.WriteLine("n");

foreach (string name in fruitNames)

{

Console.Write(name + " ");

}

Console.WriteLine("n");

}

}

Output

The output of the above program will be:

10 18 34

Apple Banana Cherry

Summary

In summary, SortedSet is an effective way to store elements in a sorted fashion, utilizing the System.Collections.Generic namespace for implementation. For further tutorials, please visit C-Sharp Tutorial, and if you found this article helpful, consider showing your appreciation by following the author.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Top 10 Free-To-Play Xbox Games of 2022 You Can't Miss

Discover the top 10 free-to-play Xbox games of 2022, offering excitement and engagement for all players.

# Navigating Weight Loss During a Holiday: My 100-Day Journey

Reflecting on my 100-day weight loss challenge while on holiday, sharing insights on emotional eating and maintaining balance.

# Understanding the Rise of COVID-19 Cases Among the Vaccinated

Explore the reasons behind the increase in COVID-19 cases among vaccinated individuals, emphasizing the importance of context and understanding base rates.