C# | Add the specified key and value into the ListDictionary
ListDictionary.Add(Object, Object) method is used to add an entry with the specified key and value into the ListDictionary.
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:
- ArgumentNullException : If the key is null.
- ArgumentException : It is an entry with the same key already exists in the ListDictionary.
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 ListDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a ListDictionary named myDict ListDictionary myDict = new ListDictionary(); 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"); // Displaying the total number of elements in myDict Console.WriteLine("Total number of elements in myDict are : " + myDict.Count); // Displaying the elements in ListDictionary myDict foreach(DictionaryEntry de in myDict) { Console.WriteLine(de.Key + " " + de.Value); } } } |
Output:
Total number of elements in myDict are : 6 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 ListDictionary using System; using System.Collections; using System.Collections.Specialized; class GFG { // Driver code public static void Main() { // Creating a ListDictionary named myDict ListDictionary myDict = new ListDictionary(); myDict.Add("Australia", "Canberra"); myDict.Add("Belgium", "Brussels"); // This should raise "ArgumentNullException" // as key is null myDict.Add(null, "Amsterdam"); myDict.Add("China", "Beijing"); myDict.Add("Russia", "Moscow"); myDict.Add("India", "New Delhi"); // Displaying the total number of elements in myDict Console.WriteLine("Total number of elements in myDict are : " + myDict.Count); // Displaying the elements in ListDictionary myDict foreach(DictionaryEntry de in myDict) { Console.WriteLine(de.Key + " " + de.Value); } } } |
Output:
Unhandled Exception:
System.ArgumentNullException: Key cannot be null.
Parameter name: key
Note:
- An object that has no correlation between its state and its hash code value should typically not be used as the key. For example, String objects are better than StringBuilder objects for use as keys.
- This method is an O(n) operation, where n is Count.
Reference:
Recommended Posts:
- C# | Get or set the value associated with specified key in ListDictionary
- C# | ListDictionary Class
- How to create a ListDictionary in C#
- C# | Check if ListDictionary is read-only
- C# | Check if ListDictionary contains a specific key
- C# | Get an enumerator that iterates through the ListDictionary
- How to get Synchronize access to the ListDictionary in C#
- C# | Get an ICollection containing the keys in ListDictionary
- C# | Remove all entries from the ListDictionary
- C# | Remove the entry with specified key from ListDictionary
- C# | Get an ICollection containing the values in ListDictionary
- C# | Check if ListDictionary has a fixed size
- C# | Get the number of key/value pairs contained in ListDictionary
- C# | Check if two ListDictionary objects are equal
- C# | Check if ListDictionary is synchronized (thread safe)
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.



