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:
// 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); } } } |
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:
// 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); } } } |
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:
Recommended Posts:
- C# | Add key and value into StringDictionary
- C# | Get or set the value associated with the specified key in StringDictionary
- C# | StringDictionary Class
- How to create a StringDictionary in C#
- C# | Get an enumerator that iterates through the stringDictionary
- C# | Remove entry with specified key from the StringDictionary
- C# | Check if the StringDictionary contains a specific key
- C# | Get the number of key/value pairs in the StringDictionary
- C# | Check if the StringDictionary contains a specific value
- C# | Removing all entries from the StringDictionary
- How to get Synchronize access to the StringDictionary in C#
- C# | Get a collection of values in the StringDictionary
- C# | Get a collection of keys in the StringDictionary
- C# | Copy StringDictionary to Array at the specified index
- C# | Check if two StringDictionary objects are equal or not
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.