Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
C# SortedDictionary Class
Next article icon

C# SortedList vs SortedDictionary

Last Updated : 31 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C# both, SortedList and SortedDictionary store key-value pairs in a sorted order. Understanding the main difference between SortedList and SortedDictionary plays a very important role in choosing the right collection for our use case. The main difference between SortedList and SortedDictionary is:

  • SortedList: It uses an array internally which makes it more memory-efficient but slower when comes to insertion and deletion(as elements need to be shifted to maintain order).
  • SortedDictionary: It uses a balanced tree structure (like a red-black tree) which allows fast insertion and deletion but at the cost of higher memory usage.

SortedList vs SortedDictionary

Features

SortedList

SortedDictionary


Underlying Data Structure

SortedList uses an array internally.

SortedDictionary uses a self-balancing tree (like a red-black tree) internally.

Indexed Access

SortedList supports indexed access.

SortedDictionary does not support indexed access.

Key-value Access

Access by index and key

Access only by key

Use Cases

SortedList is suitable for small, static collections.

SortedDictionary is suitable for large, dynamic collections.

Memory Efficiency

SortedList requires less memory.

SortedDictionary requires more memory.

Memory Fragmentation

In SortedList memory fragmentation can be higher because elements are in array.

In SortedDictionary memory fragmentationn is lower because memory is allocated dynamically.

C# SortedList

In C#, SortedList is a collection of key-value pairs, where the keys are automatically sorted in asscending order. It ensues that the key-value pairs remain sorted as we add, remove or modify items in the collection. SortedList is available in both generic and non generic types:

  • Generic SortedList<Tkey, TValue>: It is defined in the System.Collections.Generic namespace.
  • Non-Generic SortedList: It is defined in the System.Collections namespace.

Example: This example demosntrates how to create a non-generic SortedList in C#.

C#
// C# program to illustrate how // to create a SortedList using System; using System.Collections;  class Geeks {      static public void Main()     {         // Creating a non-generic SortedList         // Using SortedList class         SortedList sl = new SortedList();          // Adding key/value pairs in         // SortedList using Add() method         sl.Add(4, "C++");         sl.Add(2, "C#");         sl.Add(3, "Js");         sl.Add(1, "Java");          // Displaying SortedList elements         foreach(DictionaryEntry e in sl)         {             Console.WriteLine("Key: {0}, Value: {1}", e.Key,                               e.Value);         }         Console.WriteLine();     } } 

Output
Key: 1, Value: Java Key: 2, Value: C# Key: 3, Value: Js Key: 4, Value: C++

C# SortedDictionary

In C# SortedDictionary is a collection that stores key-value pairs in a sorted order based on the keys. SortedDictionary is a generic collection which means we can modify the types for both the keys and values. It is dynamic in nature, the size of the dictionary adjusts dynamically based on the number of elements added or removed. SortedDictionary is available in generic type:

SortedDictionary<TKey, TValue>: It is defined in the System.Collections.Generic namespace.

Example: This example demosntrates how to create a SortedDictionary in C#.

C#
// C# program to demonstrate how to  // create a SortedDictionary using System; using System.Collections.Generic;  class Geeks {     static void Main()     {         // Create a SortedDictionary with int as key         // and string as value         SortedDictionary<int, string> sd = new SortedDictionary<int, string>();          // Add key/value pairs to the SortedDictionary         sd.Add(4, "C");         sd.Add(3, "C++");         sd.Add(1, "Java");         sd.Add(5, "Ruby");         sd.Add(2, "JavaScript");          // Display the elements in the SortedDictionary         Console.WriteLine("SortedDictionary:");          // Access key/value pairs using foreach loop         foreach (KeyValuePair<int, string> entry in sd)         {             Console.WriteLine("Rank {0}: {1}", entry.Key, entry.Value);         }     } } 

Output
SortedDictionary: Rank 1: Java Rank 2: JavaScript Rank 3: C++ Rank 4: C Rank 5: Ruby 


Next Article
C# SortedDictionary Class
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp SortedDictionary Class
  • CSharp-Collections-SortedList

Similar Reads

  • C# Hashtable vs Dictionary
    In C# both Hashtable and Dictionary are used to store key-value pairs. Understanding the difference between Hashtable and Dictionary plays a very important role in choosing the right data structure for our C# applications. The main difference between Hashtable and Dictionary is: Hashtable: This is a
    3 min read
  • C# SortedDictionary Class
    In C#, the SortedDictionary<TKey,TValue> class represents the collection of key/value pairs. This pair is in sorted form and the sorting is done on the key. This class is defined under System.Collections.Generic namespace. In the SortedDictionary class, the keys are immutable, always unique, a
    5 min read
  • C# | SortedDictionary.Values Property
    This property is used to get a collection containing the values in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.ValueCollection Values { get; } Return Value: It returns a collection containing the values in the SortedDictionary. Below are the pr
    2 min read
  • C# SortedList Class
    SortedList class in C# is a collection of key-value pairs that are sorted by keys. By default, it sorts the key-value pairs in ascending order. It is both a generic and non-generic type, of collection. The generic SortedList is defined in the System.Collections.Generic namespace whereas non-generic
    6 min read
  • C# | How to create a SortedList
    SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. Properties of SortedList: Internally the object of SortedList maintains two arr
    2 min read
  • C# OrderedDictionary Class
    In C#, the OrderedDictionary Class represents a collection of key/value pairs that are accessible by the key or index. It is present in System.Collections.Specialized namespace. It implements both IDicitonary and ICollection interfaces.Allows indexed access to elements via both keys and numeric inde
    6 min read
  • C# | Search in a SortedList object
    SortedList class is a collection of (key, value) pairs which are sorted according to keys. Those pairs can be accessible by key and as well as by index(zero-based indexing). This comes under System.Collections namespace. SortedList.ContainsKey(Object) method is used to check whether a SortedList obj
    3 min read
  • C# | SortedDictionary.Add() Method
    This is used to add a specified key and value to the sorted dictionary. The elements are sorted according to TKey. Syntax: public void Add (TKey key, TValue value); Parameters: key: It is the key of the element to add. value: It is the value of the element to add. The value can be null for reference
    3 min read
  • C# | SortedDictionary.Clear() Method
    This method is used to remove all key/value pairs from the SortedDictionary<TKey, TValue>. Syntax: public void Clear (); Below are the programs to illustrate the use of the above-discussed method: Example 1: // C# code to remove all pairs // from SortedDictionary using System; using System.Col
    2 min read
  • C# | SortedDictionary.Keys Property
    This property is used to get a collection containing the keys in the SortedDictionary. Syntax: public System.Collections.Generic.SortedDictionary<TKey,TValue>.KeyCollection Keys { get; } Return Value : It returns a collection containing the keys in the SortedDictionary. Below are the programs
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences