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# StringDictionary Class
Next article icon

C# ListDictionary Class

Last Updated : 03 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, the ListDictionary class is the part of the System.Collections.Specialized namespace. It is a collection that stores key-value pairs. It is used in scenarios where the number of elements is relatively small and the order of insertion needs to be preserved.

  • It uses a single LinkedList to store key-value pairs.
  • It preserves insertion order.
  • The key cannot be null, but values can be null.
  • It is not suitable for large datasets due to performance limitations.

Example: This example demonstrates how to add elements to a ListDictionary and then display its elements.

C#
// C# program to add elements to a ListDictionary using System; using System.Collections.Specialized; using System.Collections;  class Geeks {     static void Main()     {         // Create a new ListDictionary         ListDictionary ld = new ListDictionary();          // Add key-value pairs to the ListDictionary         ld.Add("Geek1", 1);         ld.Add("Geek2", 2);         ld.Add("Geek3", 3);          // Display the elements in the ListDictionary         Console.WriteLine("Elements in the ListDictionary:");         foreach (DictionaryEntry i in ld)         {             Console.WriteLine($"{i.Key}: {i.Value}");         }     } } 

Output
Elements in the ListDictionary: Geek1: 1 Geek2: 2 Geek3: 3 

Declarartion of ListDictionary

In C#, the declaration of Dictionary can be done as:

ListDictionary variableName = new ListDictionary();

Constructors

ConstructorsDescription
ListDictionary()Creates an empty ListDictionary using the default comparer.
ListDictionary(IComparer)Creates an empty ListDictionary using the specified compare

Example: This example demonstrates how to retrieve the count of pairs and displaying the key-value pair.

C#
// C# program to demonstrates the  // working of Count property using System; using System.Collections; using System.Collections.Specialized;  class Geeks {      public static void Main()     {          // Creating a ListDictionary named ld          ListDictionary ld = new ListDictionary();          // Adding key/value pairs         ld.Add("Java", "1");         ld.Add("C++", "2");         ld.Add("Js", "3");         ld.Add("Python", "4");          // To get count of key-value pairs         Console.WriteLine("Total number of Key-value pairs: "                            + ld.Count);          // Displaying the key-value pairs         Console.WriteLine("Displaying the key-value pair: ");          foreach(DictionaryEntry i in ld)         {             Console.WriteLine("Key: "+i.Key + " , "+ "Value: "                                + i.Value);         }     } }  

Output
Total number of Key-value pairs: 4 Displaying the key-value pair:  Key: Java , Value: 1 Key: C++ , Value: 2 Key: Js , Value: 3 Key: Python , Value: 4 

Properties

PropertiesDescription
CountGets the number of key/value pairs contained in the ListDictionary.
IsFixedSizeGets a value indicating whether the ListDictionary has a fixed size.
IsReadOnlyGets a value indicating whether the ListDictionary is read-only.
IsSynchronizedGets a value indicating whether the ListDictionary is synchronized (thread safe).
Item[Object]Gets or sets the value associated with the specified key.
KeysGets an ICollection containing the keys in the ListDictionary.
SyncRootGets an object that can be used to synchronize access to the ListDictionary.
ValuesGets an ICollection containing the values in the ListDictionary.

Example 1: This example demonstrates how to retrieve the count of pairs.

C#
// C# Program to get the number  // of key/value pairs contained  // in the ListDictionary  using System; using System.Collections; using System.Collections.Specialized;  class Geeks {      public static void Main()     {         // Creating a ListDictionary named ld          ListDictionary ld = new ListDictionary();          // Adding key/value pairs          ld.Add("100", "1");         ld.Add("200", "2");         ld.Add("300", "3");         ld.Add("400", "4");          // Displaying the number of key/value          // pairs contained in the ListDictionary          Console.WriteLine(ld.Count);     } } 

Output
4 

Example 2: This example checks if the dictionary is read-only.

C#
// C# program demonstrates the  // use of IsReadOnly properity using System; using System.Collections; using System.Collections.Specialized;  class Geeks {      public static void Main() {         // Creating a ListDictionary named ld         ListDictionary ld = new ListDictionary();          // Adding key/value pairs in l         ld.Add("100", "1");         ld.Add("200", "2");         ld.Add("300", "3");         ld.Add("400", "4");          // Checking if ListDictionary is read-only          Console.WriteLine(ld.IsReadOnly);     } } 

Output
False 

Methods

MethodsDescription
Add(Object, Object)Adds an entry with the specified key and value into the ListDictionary.
Clear()Removes all entries from the ListDictionary.
Contains(Object)Determines whether the ListDictionary contains a specific key.
CopyTo(Array, Int32)Copies the ListDictionary entries to a one-dimensional Array instance at the specified index.
Equals(Object)Determines whether the specified object is equal to the current object.
GetEnumerator()Returns an IDictionaryEnumerator that iterates through the ListDictionary.
GetHashCode()Serves as the default hash function.
GetType()Gets the Type of the current instance.
MemberwiseClone()Creates a shallow copy of the current Object.
Remove(Object)Removes the entry with the specified key from the ListDictionary.
ToString()Returns a string that represents the current object.

Example: This example demonstrates how to remove all the entries using Clear() method.

C#
// C# Program to remove all entries  // from the ListDictionary  using System; using System.Collections; using System.Collections.Specialized;  class Geeks {      public static void Main()     {         // Creating a ListDictionary named ld          ListDictionary ld = new ListDictionary();          // Adding key/value pairs in l          ld.Add("I", "first");         ld.Add("II", "second");          // To get count of key/value pairs          Console.WriteLine("Total key-value pairs in ListDictionary are: "                 + ld.Count);          // Displaying the key/value pairs          Console.WriteLine("The key-value pairs in ListDictionary are: ");          foreach(DictionaryEntry i in ld)         {             Console.WriteLine(i.Key + " " + i.Value);         }          // Removing all entries from the ListDictionary          ld.Clear();          // To get count of key/value pairs          Console.WriteLine("Total key/value pairs in ListDictionary are: "                 + ld.Count);     } }  

Output
Total key-value pairs in ListDictionary are: 2 The key-value pairs in ListDictionary are:  I first II second Total key/value pairs in ListDictionary are: 0 


Next Article
C# StringDictionary Class

S

Sahil_Bansall
Improve
Article Tags :
  • C#
  • CSharp-Specialized-ListDictionary
  • CSharp-Specialized-Namespace

Similar Reads

  • C# Dictionary Class
    In C#, the Dictionary class is the part of the System.Collections.Generic namespace. It is a Collection that stores Key-value pairs. Each key in the dictionary is unique and each key maps to a single value. In Dictionary, each entry consists of a key and its associated value.It provides constant tim
    7 min read
  • C# StringDictionary Class
    In C#, the StringDictionary class is the part of the System.Collections.Specialized namespace. It is a collection of key-value pairs where the keys are strings and values are objects. It is similar to a Hashtable. But it is specifically designed for use with string keys. Keys are always strings, and
    5 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# HybridDictionary Class
    In C#, the HybridDictionary Class is the part of the System.Collections.Specialized namespace. It is a collection that combines the features of both a Hashtable and a ListDictionary. It implements a linked list and hash table data structure. It implements IDictionary by using a ListDictionary when t
    6 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# Dictionary
    Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean
    5 min read
  • C# List Class
    In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search,
    7 min read
  • C# StringCollection Class
    In C#, the StringCollection class is part of the System.Collections.Specialized namespace. It provides a collection of strings that allows us to store and manage a list of strings easily. This class is a type-safe collection that automatically resizes as needed. StringCollection class accepts null a
    5 min read
  • C# | Collection Class
    .math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-chil
    5 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
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