C# Sorted Dictionary Tutorial with Examples

C# Sorted Dictionary Tutorial with Examples

Today, We want to share with you C# Sorted Dictionary Tutorial with Examples.
In this post we will show you Sorted Dictionary in C#.Net with example and description, hear for we will give you demo and example for implement.
In this post, we will learn about C# SortedDictionary Examples with an example.

Introduction: Sorted Dictionary in C#.Net

In this post, we will learn about Sorted Dictionary in C#.Net with an example. SortedDictionary keeps its keys always sorted. It allows you to avoid sorting the keys on your own.It has advantages if you want a sorted lookup table.

READ :  AngularJS Push values object into array First Index

Methods:

1. Add()

This method is used to add the key/value pair to the dictionary.

2. ContainKey()

This method is used to determines whether the SortedDictionary<TKey, TValue> contains an element with the specified key or not. If yes then return true else return false.

3. ContainValue()

This method is used to determines whether the SortedDictionary<TKey, TValue> contains an element with the specified value or not and returns true if the value exists.

4. Remove()

E-junkie: Sell digital downloads online

E-junkie Provides a Copy-paste buy-now, and cart buttons for selling downloads, codes and tangible products on any website, blog, social media, email and messenger!

Also see:

  1. The Top 10+ Best Webinar Software Platforms For 2020-2021
  2. Build Your Future Godaddy Careers And Jobs
  3. Introduction To Web Hosting Services

This method is used to removes the element with the specified key from the SortedDictionary<TKey, TValue>.

5. Clear()

This method is used to removes all elements from the SortedDictionary<TKey, TValue>.

READ :  vuejs insert update delete CRUD application

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 SortedDictionary with site as a string and Int16 value pair
            SortedDictionary SiteList = new SortedDictionary();
            SiteList.Add("DotNetK.com", 10);
            SiteList.Add("Live24u.com", 30);
            SiteList.Add("w3school.com", 15);

            // Read all data
            Console.WriteLine("Site List");

            foreach (KeyValuePair site in SiteList)
            {
                Console.WriteLine("Key: {0}, Value: {1}",
                    site.Key, site.Value);
            }
            Console.ReadKey();
        }
    }   
}

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about ASP.NET C# uses for SortedList vs. SortedDictionary vs. Dictionary.
I would like to have feedback on my Infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

READ :  C# Shortest Superstring Problem Algorithms