C# | Add the specified key and value into the ListDictionary Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: CSHARP // 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: CSHARP // 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: https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.listdictionary.add?view=netframework-4.7.2 Comment More infoAdvertise with us Next Article C# | Adding the specified key and value into HybridDictionary S Sahil_Bansall Follow Improve Article Tags : C# CSharp-method CSharp-Specialized-Namespace CSharp-Specialized-ListDictionary Similar Reads C# | Add key and value into StringDictionary StringDictionary.Add(String, String) method is used to add an entry with the specified key and value into the StringDictionary. Syntax: public virtual void Add (string key, string value); Parameters: key: It is the key of the entry which is to be added. value: It is the value of the entry which is t 2 min read C# | Get or set the value associated with specified key in ListDictionary ListDictionary.Item[Object] property is used to get or set the value associated with the specified key. Syntax: public object this[object key] { get; set; } Here, key is the key whose value to get or set. Return Value : The value associated with the specified key. If the specified key is not found, 2 min read 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: 3 min read C# | Remove the entry with specified key from ListDictionary ListDictionary.Remove(Object) method is used to remove the entry with the specified key from the ListDictionary. Syntax: public void Remove (object key); Here, key is the key of the entry which is to be removed. Exception: This method will give ArgumentNullException if the key is null. Below are the 3 min read C# | Copy ListDictionary to Array instance at the specified index ListDictionary.CopyTo(Array, Int32) method is used to copy the ListDictionary entries to a one-dimensional Array instance at the specified index. Syntax: public void CopyTo (Array array, int index); Parameters: array : It is the one-dimensional Array which is the destination of the DictionaryEntry o 3 min read C# | Insert into OrderedDictionary with key and value at specified index OrderedDictionary.Insert(Int32, Object, Object) method is used to insert a new entry into the OrderedDictionary collection with the specified key and value at the specified index. Syntax: public void Insert (int index, object key, object value); Parameters: index : It is the zero-based index at whic 3 min read Like