C# | Gets or sets the value at the specified key in StringDictionary

StringDictionary.Item[String] Property is used to get or set the value associated with the specified key.

Syntax:

public virtual string this[string key] { get; set; }

Here, key is the Key of type System.String whose value is be to get or set.



Return Value: This property returns the value associated with the specified key. If the specified key is not found, Get returns null, and Set creates a new entry with the specified key.

Exception: This property throws ArgumentNullException if the key is null.

Below programs illustrate the use of above-discussed property:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get or set the value at
// the specified key in StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named my1
        StringDictionary my1 = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        my1.Add("1", "C");
        my1.Add("2", "C++");
        my1.Add("3", "Java");
        my1.Add("4", "Python");
        my1.Add("5", "C#");
  
        // Displaying the keys and
        // values in StringDictionary
        foreach(DictionaryEntry d in my1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
  
        Console.WriteLine("\nAfter Item[String] Property: \n");
  
        // setting the value at key 2
        my1["2"] = "HTML";
  
        // Displaying the keys and
        // values in StringDictionary
        foreach(DictionaryEntry d1 in my1)
        {
            Console.WriteLine(d1.Key + " " + d1.Value);
        }
    }
}

chevron_right


Output:

3 Java
5 C#
4 Python
2 C++
1 C

After Item[String] Property: 

3 Java
4 Python
2 HTML
1 C
5 C#

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to get or set the value at
// the specified key in StringDictionary
using System;
using System.Collections;
using System.Collections.Specialized;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a StringDictionary named my1
        StringDictionary my1 = new StringDictionary();
  
        // Adding key and value into the StringDictionary
        my1.Add("1", "HTML");
        my1.Add("2", "CSS");
        my1.Add("3", "PHP");
        my1.Add("4", "MongoDB");
        my1.Add("5", "AngularJS");
  
        // Displaying the keys and
        // values in StringDictionary
        foreach(DictionaryEntry d in my1)
        {
            Console.WriteLine(d.Key + " " + d.Value);
        }
  
        Console.WriteLine("\nAfter Item[String] Property: \n");
  
        // setting the value at Key 8
        // here key 8 is not present
        // so it will add a new key/value
        // pair. see output
        my1["8"] = "C#";
  
        // Displaying the keys and
        // values in StringDictionary
        foreach(DictionaryEntry d1 in my1)
        {
            Console.WriteLine(d1.Key + " " + d1.Value);
        }
    }
}

chevron_right


Output:

3 PHP
5 AngularJS
4 MongoDB
2 CSS
1 HTML

After Item[String] Property: 

3 PHP
4 MongoDB
2 CSS
1 HTML
8 C#
5 AngularJS

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.