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() Method in SortedDictionary
The Add() method is used to add the key/value pairto the dictionary.
Example
using System;
using System.Collections.Generic;
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sortedDictionary = new SortedDictionary<int, string>();
sortedDictionary.Add(1, "January");
sortedDictionary.Add(2, "February");
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 in SortedDictionary
Determines whether the SortedDictionary<TKey, TValue> contains an element with the specified key or not. If yes then return true else return false.
Example
using System;
using System.Collections.Generic;
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sortedDictionary = new SortedDictionary<int, string>();
sortedDictionary.Add(1, "January");
sortedDictionary.Add(2, "February");
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 contains in the sorted dictionary: " + sortedDictionary.ContainsKey(12));
Console.WriteLine("The key 4 contains in the sorted dictionary: " + sortedDictionary.ContainsKey(4));
Console.WriteLine("The key 14 contains in the sorted dictionary: " + sortedDictionary.ContainsKey(14));
}
}
}
OUTPUT

ContainValue() Method in sortedDictionary
Determines whether the SortedDictionary<TKey, TValue> contains an element with the specified value or not and returns true if the value exists.
Example
using System;
using System.Collections.Generic;
namespace SortedDictionary1
{
class Program
{
static void Main(string[] args)
{
SortedDictionary<int, string> sortedDictionary = new SortedDictionary<int, string>();
sortedDictionary.Add(1, "January");
sortedDictionary.Add(2, "February");
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 contains in the sorted dictionary: " + sortedDictionary.ContainsValue("April"));
Console.WriteLine("The value Sunday contains in the sorted dictionary: " + sortedDictionary.ContainsValue("Sunday"));
Console.WriteLine("The value December contains in the sorted dictionary: " + sortedDictionary.ContainsValue("December"));
}
}
}
OUTPUT

Remove() Method in sortedDictionary
Removes the element with the specified key from the SortedDictionary<TKey, TValue>.
Example






Comments
Join the conversation! Your thoughts help the community grow.