public Add ( string key, string value ) : void | ||
key | string | |
value | string | |
return | void |
StringDictionary myDictionary = new StringDictionary(); // Adding key-value pairs to StringDictionary myDictionary.Add("Name", "John Smith"); myDictionary.Add("Age", "30"); myDictionary.Add("City", "New York"); // Outputting the values Console.WriteLine(myDictionary["Name"]); Console.WriteLine(myDictionary["Age"]); Console.WriteLine(myDictionary["City"]);
Hashtable myHashtable = new Hashtable(); myHashtable.Add("Name", "John Smith"); myHashtable.Add("Age", "30"); myHashtable.Add("City", "New York"); StringDictionary myDictionary = new StringDictionary(); // Adding key-value pairs to StringDictionary from the existing hashtable foreach (DictionaryEntry item in myHashtable) { myDictionary.Add((string)item.Key, (string)item.Value); } // Outputting the values Console.WriteLine(myDictionary["Name"]); Console.WriteLine(myDictionary["Age"]); Console.WriteLine(myDictionary["City"]);In this example, we first create a Hashtable and add three key-value pairs to it. We then create a new instance of StringDictionary and add the values from the hashtable to it using a foreach loop. Finally, we output the values of each key using the indexer. Overall, the StringDictionary class is useful for storing and accessing key-value pairs of strings in a fast and efficient manner.