C# | Get or set the value associated with specified key in ListDictionary - GeeksforGeeks

01 Feb, 2019
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, attempting to get it returns null, and attempting to set it creates a new entry using the specified key.
Exception: This property will give ArgumentNullException if the key is null.
Example:
// C# code to get or set the value// associated with the specified keyusing 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();          // 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");          // Displaying the key/value pairs in myDict        foreach(DictionaryEntry de in myDict)        {            Console.WriteLine(de.Key + " " + de.Value);        }          // Displaying the value associated        // with key "Russia"        Console.WriteLine(myDict["Russia"]);          // Setting the value associated with key "Russia"        myDict["Russia"] = "Saint Petersburg";          // Displaying the value associated        // with key "Russia"        Console.WriteLine(myDict["Russia"]);          // Displaying the value associated        // with key "India"        Console.WriteLine(myDict["India"]);          // Setting the value associated with key "India"        myDict["India"] = "Mumbai";          // Displaying the value associated        // with key "India"        Console.WriteLine(myDict["India"]);          // Displaying the key/value pairs in myDict        foreach(DictionaryEntry de in myDict)        {            Console.WriteLine(de.Key + " " + de.Value);        }    }}
Output:
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
Russia Moscow
India New Delhi
Moscow
Saint Petersburg
New Delhi
Mumbai
Australia Canberra
Belgium Brussels
Netherlands Amsterdam
China Beijing
Russia Saint Petersburg
India Mumbai

Note:
This property provides the ability to access a specific element in the collection by using the syntax : myCollection[key].
A key cannot be null, but a value can.
This method is an O(n) operation, where n is Count.
Reference:
https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.listdictionary.item?view=netframework-4.7.2
CSharp-Specialized-ListDictionary
CSharp-Specialized-Namespace
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Method Overriding
Destructors in C#
Difference between Ref and Out keywords in C#
C# | Delegates
C# | String.IndexOf( ) Method | Set - 1
C# | Constructors
Extension Method in C#
Introduction to .NET Framework
C# | Class and Object
C# | Abstract Classes