shows some methods on the StringDictionary. The constructor is called, and then Add is invoked. Various other features of the type are then used.
using System;
using System.Collections;
using System.Collections.Specialized;
class Program
{
static void Main()
{
// Create new StringDictionary.
StringDictionary dict = new StringDictionary();
dict.Add(
"cat",
"feline");
dict.Add(
"dog",
"canine");
// Try the indexer.
Console.WriteLine(dict[
"cat"]);
Console.WriteLine(dict[
"test"] == null);
// Use ContainsKey.
Console.WriteLine(dict.ContainsKey(
"cat"));
Console.WriteLine(dict.ContainsKey(
"puppet"));
// Use ContainsValue.
Console.WriteLine(dict.ContainsValue(
"feline"));
Console.WriteLine(dict.ContainsValue(
"perls"));
// See if it is thread-safe.
Console.WriteLine(dict.IsSynchronized);
// Loop through pairs.
foreach (DictionaryEntry entry in dict)
{
Console.WriteLine(
"{0} = {1}", entry.Key, entry.Value);
}
// Loop through keys.
foreach (string key in dict.Keys)
{
Console.WriteLine(key);
}
// Loop through values.
foreach (string value in dict.Values)
{
Console.WriteLine(value);
}
// Use Remove method.
dict.Remove(
"cat");
Console.WriteLine(dict.Count);
// Use Clear method.
dict.Clear();
Console.WriteLine(dict.Count);
}
}
feline
True
True
False
True
False
False
dog = canine
cat = feline
dog
cat
canine
feline
1
0