Open In App

C# | Adding the specified key and value into HybridDictionary

HybridDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the HybridDictionary.

Syntax:



public void Add (object key, object value);

Parameters:

key : The key of the entry to add.
value : The value of the entry to add. The value can be null.



Exceptions:

Below given are some examples to understand the implementation in a better way :

Example 1:




// C# code to add an entry with
// the specified key and value
// into the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("Australia", "Canberra");
        myDict.Add("Belgium", "Brussels");
        myDict.Add("Netherlands", "Amsterdam");
        myDict.Add("China", "Beijing");
        myDict.Add("Russia", "Moscow");
        myDict.Add("India", "New Delhi");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
  
        // Displaying the key/value pairs in myDict
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " --> " + de.Value);
        }
    }
}

Output:

Total key/value pairs in myDict are : 6
The key/value pairs in myDict are : 
Australia --> Canberra
Belgium --> Brussels
Netherlands --> Amsterdam
China --> Beijing
Russia --> Moscow
India --> New Delhi

Example 2:




// C# code to add an entry with
// the specified key and value
// into the HybridDictionary.
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a HybridDictionary named myDict
        HybridDictionary myDict = new HybridDictionary();
  
        // Adding key/value pairs in myDict
        myDict.Add("I", "first");
        myDict.Add("II", "second");
        myDict.Add("III", "third");
        myDict.Add("IV", "fourth");
        myDict.Add("V", "fifth");
  
        // To get count of key/value pairs in myDict
        Console.WriteLine("Total key/value pairs in myDict are : " 
                                                  + myDict.Count);
  
        // Displaying the key/value pairs in myDict
        Console.WriteLine("The key/value pairs in myDict are : ");
  
        foreach(DictionaryEntry de in myDict)
        {
            Console.WriteLine(de.Key + " --> " + de.Value);
        }
    }
}

Output:

Total key/value pairs in myDict are : 5
The key/value pairs in myDict are : 
I --> first
II --> second
III --> third
IV --> fourth
V --> fifth

Note:

Reference:


Article Tags :
C#