Working on SortedDictionary in C#

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

PIC1.jpg

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

PIC2.jpg

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

pic3.jpg

Remove() Method in sortedDictionary

Removes the element with the specified key from the SortedDictionary<TKey, TValue>.

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);
            }

            sortedDictionary.Remove(10);
            sortedDictionary.Remove(11);
            sortedDictionary.Remove(12);

            Console.WriteLine("After removal, the keys in the sorted dictionary are:");

            foreach (KeyValuePair<int, string> pair in sortedDictionary)
            {
                Console.WriteLine("{0}, {1}", pair.Key, pair.Value);
            }
        }
    }
}

OUTPUT

PIC4.jpg

Clear() Method in sortedDictionary

Removes all elements from the SortedDictionary<TKey, TValue>.

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("Before applying the Clear method, the number of items in the sorted dictionary: " + sortedDictionary.Count);

            sortedDictionary.Clear();

            Console.WriteLine("After applying the Clear method, the number of items in the sorted dictionary: " + sortedDictionary.Count);
        }
    }
}

OUTPUT

PIC5.jpg

Properties of SortedDictionary


Keys

Gets a collection containing the keys in the SortedDictionary<TKey, TValue>.

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 (int n in sortedDictionary.Keys)
            {
                Console.WriteLine("key={0}", n);
            }
        }
    }
}

OUTPUT

pic6.jpg

Values

Gets a collection containing the values in the SortedDictionary<TKey, TValue>.

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 (string str in sortedDictionary.Values)
            {
                Console.WriteLine(str);
            }
        }
    }
}

OUTPUT

PIC7.jpg

Count

Gets the number of key/value pairs contained in the SortedDictionary<TKey, TValue>.

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");

            Console.WriteLine("The total number of key/value pairs in the sorted dictionary is: " + sortedDictionary.Count);
        }
    }
}

OUTPUT

PIC8.jpg

Summary

In this article I explained the concept of the SortedDictionary<> class and the various methods and properties of this class.


Recommended Free Ebook
Similar Articles