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 implemented with a linked list. This class is a simple implementation of a dictionary collection (System.Collections.IDictionary) for small lists.

Syntax:

public ListDictionary ();

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

The number of elements: 0

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to illustrate how
// to create a ListDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class Geeks {
  
    // Main Method
    public static void Main(String[] args)
    {
  
        // ld is the ListDictionary object
        // ListDictionary() is the constructor
        // used to initializes a new
        // instance of the ListDictionary class
        ListDictionary ld = new ListDictionary();
  
        Console.Write("Before Add Method: ");
  
        // Count property is used to get the
        // number of elements in ListDictionary
        // It will give 0 as no elements
        // are present currently
        Console.WriteLine(ld.Count);
  
        // Adding key/value pairs in ld
        ld.Add("Australia", "Canberra");
        ld.Add("Belgium", "Brussels");
        ld.Add("Netherlands", "Amsterdam");
        ld.Add("China", "Beijing");
        ld.Add("Russia", "Moscow");
        ld.Add("India", "New Delhi");
  
        Console.Write("After Add Method: ");
  
        // Count property is used to get the
        // number of elements in ld
        Console.WriteLine(ld.Count);
    }
}

chevron_right


Output:

Before Add Method: 0
After Add Method: 6

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.