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:
How to create a StringDictionary in C#
Next article icon

How to create an OrderedDictionary in C#

Last Updated : 27 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

OrderedDictionary() constructor is used to initialize a new instance of the OrderedDictionary class which will be empty and will have the default initial capacity. 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.

Syntax:

public OrderedDictionary();

Example 1:




// C# Program to illustrate how
// to create a OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // od is the OrderedDictionary object
        // OrderedDictionary() is the constructor
        // used to initializes a new
        // instance of the OrderedDictionary class
        OrderedDictionary od = new OrderedDictionary();
  
        // Count property is used to get the
        // number of elements in OrderedDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine("The number of elements: "
                                           +od.Count);
    }
}
 
 
Output:
  The number of elements: 0  

Example 2:




// C# Program to illustrate how
// to create a OrderedDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // od is the OrderedDictionary object
        // OrderedDictionary() is the constructor
        // used to initializes a new
        // instance of the OrderedDictionary class
        OrderedDictionary od = new OrderedDictionary();
  
        Console.Write("Before Add Method: ");
  
        // Count property is used to get the
        // number of elements in OrderedDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(od.Count);
  
        // Adding key/value pairs in od
        od.Add("1", "C");
        od.Add("2", "C++");
        od.Add("3", "Java");
        od.Add("4", "C#");
  
        Console.Write("After Add Method: ");
  
        // Count property is used to get the
        // number of elements in ld
        Console.WriteLine(od.Count);
    }
}
 
 
Output:
  Before Add Method: 0  After Add Method: 4  

Reference:

  • https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.ordereddictionary.-ctor?view=netframework-4.7.2#System_Collections_Specialized_OrderedDictionary__ctor


Next Article
How to create a StringDictionary in C#

K

Kirti_Mangal
Improve
Article Tags :
  • C#
  • CSharp-Specialized-Namespace
  • CSharp-Specialized-OrderedDictionary

Similar Reads

  • How to create a ListDictionary in C#
    ListDictionary() constructor is used to initialize a new empty instance of the ListDictionary class using the default comparer. ListDictionary is a specialized collection. It comes under the System.Collections.Specialized namespace. This type represents a non-generic dictionary type. It is implement
    2 min read
  • How to create a StringDictionary in C#
    StringDictionary() constructor is used to initialize a new instance of the StringDictionary class which will be empty and will have the default initial capacity. StringDictionary is a specialized collection. This class comes under the System.Collections.Specialized namespace. It only allows string k
    2 min read
  • How to create a StringCollection in C#
    StringCollection() constructor is used to initializing a new instance of the StringCollection class. StringCollection class is a new addition to the .NET Framework class library that represents a collection of strings. StringCollection class is defined in the System.Collections.Specialized namespace
    2 min read
  • C# | Get a read-only copy of the OrderedDictionary
    OrderedDictionary.AsReadOnly method returns a read-only copy of the current OrderedDictionary collection. Syntax: public System.Collections.Specialized.OrderedDictionary AsReadOnly (); Return Value: A read-only copy of the current OrderedDictionary collection. Below given are some examples to unders
    2 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# | How to create a SortedSet
    SortedSet class represents the collection of objects in sorted order. This class comes under the System.Collections.Generic namespace. Properties : In C#, SortedSet class can be used to store, remove or view elements. It maintains ascending order and does not store duplicate elements. It is suggeste
    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# | 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# | Add key and value into OrderedDictionary collection
    OrderedDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the OrderedDictionary collection with the lowest available index. Syntax: public void Add (object key, object value); Parameters: key : The key of the entry to add. value : The value of the ent
    2 min read
  • C# | Remove all elements from OrderedDictionary
    OrderedDictionary.Clear method is used to remove all elements from the OrderedDictionary collection. Syntax: public void Clear (); Exception: If the OrderedDictionary collection is read-only then it will throw NotSupportedException. Below given are some examples to understand the implementation in a
    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