Open In App

How to create a ListDictionary in C#

Last Updated : 27 Feb, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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: csharp
// 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);
    }
}
Output:
The number of elements: 0
Example 2: csharp
// 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);
    }
}
Output:
Before Add Method: 0
After Add Method: 6
Reference:

Next Article

Similar Reads