Introduction
A SortedDictionary<Tkey,Tvalue> represents a collection of key/value pairs that are sorted on the key. The sorted collection classes differ from unsorted implementation collections in that the elements are sorted by key for SortedDictionary<Tkey,Tvalue> and by value for SortedList<t>. The hierarchy of this class is System.Collections.Generic.SortedDictionary<TKey,TValue>, where Tkey is the type of the keys in the dictionary and Tvalue is the type of the values in the dictionary.
Creation of SortedDictionary
To create the SortedDictionary, we write the following:
SortedDictionary<int,string> sortedDictionary = new SortedDictionary<int, string>();
Methods of SortedDictionary
Add()
The Add() method is used to add the key/value pair to the dictionary.
For example:
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sorteddictionary = new SortedDictionary<int, string>();
sorteddictionary.Add(1, "January");
sorteddictionary.Add(2, "Feburary");
sorteddictionary.Add(3, "March");
sorteddictionary.Add(4, "April");
sorteddictionary.Add(5, "May");
sorteddictionary.Add(6, "June");
sorteddictionary.Add(7, "July");
sorteddictionary.Add(8, "August");
sorteddictionary.Add(9, "September");
sorteddictionary.Add(10, "October");
sorteddictionary.Add(11, "November");
sorteddictionary.Add(12, "December");
foreach (KeyValuePair<int, string> pair in sorteddictionary)
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
}
}
}
OUTPUT
ContainKey() Method
Determines whether the SortedDictionary<TKey, TValue> contains an element with the specified key or not. If yes then return true else return false.
For example:
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sorteddictionary = new SortedDictionary<int, string>();
sorteddictionary.Add(1, "January");
sorteddictionary.Add(2, "Feburary");
sorteddictionary.Add(3, "March");
sorteddictionary.Add(4, "April");
sorteddictionary.Add(5, "May");
sorteddictionary.Add(6, "June");
sorteddictionary.Add(7, "July");
sorteddictionary.Add(8, "August");
sorteddictionary.Add(9, "September");
sorteddictionary.Add(10, "October");
sorteddictionary.Add(11, "November");
sorteddictionary.Add(12, "December");
foreach (KeyValuePair<int, string> pair in sorteddictionary)
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
Console.WriteLine("The key 12 contain in the sorteddictionary:" + sorteddictionary.ContainsKey(12));
Console.WriteLine("The key 4 contain in the sorteddictionary:" + sorteddictionary.ContainsKey(4));
Console.WriteLine("The key 14 contain in the sorteddictionary:" + sorteddictionary.ContainsKey(14));
}
}
}
OUTPUT
ContainValue() Method
Determines whether the SortedDictionary<TKey, TValue> contains an element with the specified value or not and returns true if the value exists.
For example:
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sorteddictionary = new SortedDictionary<int, string>();
sorteddictionary.Add(1, "January");
sorteddictionary.Add(2, "Feburary");
sorteddictionary.Add(3, "March");
sorteddictionary.Add(4, "April");
sorteddictionary.Add(5, "May");
sorteddictionary.Add(6, "June");
sorteddictionary.Add(7, "July");
sorteddictionary.Add(8, "August");
sorteddictionary.Add(9, "September");
sorteddictionary.Add(10, "October");
sorteddictionary.Add(11, "November");
sorteddictionary.Add(12, "December");
foreach (KeyValuePair<int, string> pair in sorteddictionary)
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
Console.WriteLine("The value April contain in the sorteddictionary:" + sorteddictionary.ContainsValue("April"));
Console.WriteLine("The value Sunday contain in the sorteddictionary:" + sorteddictionary.ContainsValue("Sunday"));
Console.WriteLine("The value December contain in the sorteddictionary:" + sorteddictionary.ContainsValue("December"));
}
}
}
OUTPUT
Remove() Method
Removes the element with the specified key from the SortedDictionary<TKey, TValue>.
For example:
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sorteddictionary = new SortedDictionary<int, string>();
sorteddictionary.Add(1, "January");
sorteddictionary.Add(2, "Feburary");
sorteddictionary.Add(3, "March");
sorteddictionary.Add(4, "April");
sorteddictionary.Add(5, "May");
sorteddictionary.Add(6, "June");
sorteddictionary.Add(7, "July");
sorteddictionary.Add(8, "August");
sorteddictionary.Add(9, "September");
sorteddictionary.Add(10, "October");
sorteddictionary.Add(11, "November");
sorteddictionary.Add(12, "December");
foreach (KeyValuePair<int, string> pair in sorteddictionary)
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
sorteddictionary.Remove(10);
sorteddictionary.Remove(11);
sorteddictionary.Remove(12);
Console.WriteLine("After removal the keys the sorteddictionary is as.");
foreach (KeyValuePair<int, string> pair in sorteddictionary)
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
}
}
}
OUTPUT
Clear() Method
Removes all elements from the SortedDictionary<TKey, TValue>.
For example:
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sorteddictionary = new SortedDictionary<int, string>();
sorteddictionary.Add(1, "January");
sorteddictionary.Add(2, "Feburary");
sorteddictionary.Add(3, "March");
sorteddictionary.Add(4, "April");
sorteddictionary.Add(5, "May");
sorteddictionary.Add(6, "June");
sorteddictionary.Add(7, "July");
sorteddictionary.Add(8, "August");
sorteddictionary.Add(9, "September");
sorteddictionary.Add(10, "October");
sorteddictionary.Add(11, "November");
sorteddictionary.Add(12, "December");
foreach (KeyValuePair<int, string> pair in sorteddictionary)
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
Console.WriteLine("Before apply clear method the number of item in the sorteddictionary:" + sorteddictionary.Count());
sorteddictionary.Clear();
Console.WriteLine("After apply clear method the number of items in sorteddictionary:" + sorteddictionary.Count());
}
}
}
OUTPUT
Properties of SortedDictionary
Keys
Gets a collection containing the keys in the SortedDictionary<TKey, TValue>.
For example:
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sorteddictionary = new SortedDictionary<int, string>();
sorteddictionary.Add(1, "January");
sorteddictionary.Add(2, "Feburary");
sorteddictionary.Add(3, "March");
sorteddictionary.Add(4, "April");
sorteddictionary.Add(5, "May");
sorteddictionary.Add(6, "June");
sorteddictionary.Add(7, "July");
sorteddictionary.Add(8, "August");
sorteddictionary.Add(9, "September");
sorteddictionary.Add(10, "October");
sorteddictionary.Add(11, "November");
sorteddictionary.Add(12, "December");
foreach (int n in sorteddictionary.Keys)
{
Console.WriteLine("key={0}",n);
}
}
}
}
OUTPUT
Values
Gets a collection containing the values in the SortedDictionary<TKey, TValue>.
For example:
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sorteddictionary = new SortedDictionary<int, string>();
sorteddictionary.Add(1, "January");
sorteddictionary.Add(2, "Feburary");
sorteddictionary.Add(3, "March");
sorteddictionary.Add(4, "April");
sorteddictionary.Add(5, "May");
sorteddictionary.Add(6, "June");
sorteddictionary.Add(7, "July");
sorteddictionary.Add(8, "August");
sorteddictionary.Add(9, "September");
sorteddictionary.Add(10, "October");
sorteddictionary.Add(11, "November");
sorteddictionary.Add(12, "December");
foreach (string str in sorteddictionary.Values)
{
Console.WriteLine(str);
}
}
}
}
OUTPUT
Count
Gets the number of key/value pairs contained in the SortedDictionary<TKey, TValue>.
For example:
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sorteddictionary = new SortedDictionary<int, string>();
sorteddictionary.Add(1, "January");
sorteddictionary.Add(2, "Feburary");
sorteddictionary.Add(3, "March");
sorteddictionary.Add(4, "April");
sorteddictionary.Add(5, "May");
sorteddictionary.Add(6, "June");
sorteddictionary.Add(7, "July");
sorteddictionary.Add(8, "August");
sorteddictionary.Add(9, "September");
sorteddictionary.Add(10, "October");
sorteddictionary.Add(11, "November");
sorteddictionary.Add(12, "December");
Console.WriteLine("The total number of key/value pair in sorteddictionary is:" + sorteddictionary.Count());
}
}
}
OUTPUT
Summary
In this article I explained the concept of the SortedDictionary<> class and the various methods and properties of this class.