Dark Mode
Image

C# Properties

C# Inheritance

C# Polymorphism

C# Strings

C# Generics

C# Delegates

C# Reflection

Anonymous Function

C# Multithreading

C# Synchronization

C# Web Service

C# Misc

C# New Features

C# Programs

ADO.NET Tutorial

ASP.NET Tutorial

C# SortedDictionary

C# SortedDictionary class uses the concept of hashtable. It stores values on the basis of key. It contains unique keys and maintains ascending order on the basis of key. By the help of key, we can easily search or remove elements. It is found in System.Collections.Generic namespace.

C# SortedDictionary example

Let's see an example of generic SortedDictionary class that stores elements using Add() method and iterates elements using for-each loop. Here, we are using KeyValuePair class to get key and value.

using System;  
using System.Collections.Generic;  
  
public class SortedDictionaryExample  
{  
    public static void Main(string[] args)  
    {  
        SortedDictionary names = new SortedDictionary();  
        names.Add("1","Sunil");    
        names.Add("4","Anil");    
        names.Add("5","Mathew");    
        names.Add("3","Pushkar");    
        names.Add("2","sujit");    
        foreach (KeyValuePair kv in names)  
        {  
            Console.WriteLine(kv.Key+" "+kv.Value);  
        }  
    }  

Output:

1 Sunil
2 Sujit 
3 Pushkar
4 Anil
5 Mathew

 

Comment / Reply From