C# - StringDictionary, the Specialized Dictionary optmized for String

There is an type that is called the StringDictionary, the namespace is System.Collections.Specialized;

 

the StringDictionary is optmized for both key and value types are string;

 

What situation will the String dictionary be useful? the StringDictionary will be useful in scenario such as Application configuration;

 

Below shows the operation that you can apply on the StrnigDictionary; Hopefull it may help you understand the StringDictionary;

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Collections;

namespace StringDictionaryTest
{
  class Program
  {
    static void Main(string[] args)
    {

      StringDictionary myCol = new StringDictionary();
      myCol.Add("red" , "rojo");
      myCol.Add("green", "verde");
      myCol.Add("blue", "azul");

      // Display the contenst of the collection using foreach, this is preferred method
      Console.WriteLine("Display the elemnts using foreach ");
      PrintKeysAndValues1(myCol);

      // Display the contens of the collection using Enumerator
      Console.WriteLine("Display the elements using the IEnumerator:");
      PrintKeysAndValues2(myCol);


      // Display the contents of the collection using the keys, values, Count and Item properties
      Console.WriteLine("Display the elements using the Keys, Values, Count and item properties");
      PrintKeysAndValues3(myCol);

      // Copies the StringDictionary to an array with DictionaryEntry elements.
      DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count];
      myCol.CopyTo(myArr, 0);

      // Displays the values in the array.
      Console.WriteLine("Displays the elements in the array:");
      Console.WriteLine("   KEY        VALUE");
      for (int i = 0; i < myArr.Length; i++)
        Console.WriteLine("   {0,-10} {1}", myArr[i].Key, myArr[i].Value);
      Console.WriteLine();

      // Searches for a value. 
      if (myCol.ContainsValue("amarillo"))
        Console.WriteLine("The collection contains the value \"amarillo\".");
      else
        Console.WriteLine("The collection does not contain the value \"amarillo\".");
      Console.WriteLine();

      // Searches for a key and deletes it. 
      if (myCol.ContainsKey("green"))
        myCol.Remove("green");
      Console.WriteLine("The collection contains the following elements after removing \"green\":");
      PrintKeysAndValues1(myCol);

      // Clears the entire collection.
      myCol.Clear();
      Console.WriteLine("The collection contains the following elements after it is cleared:");
      PrintKeysAndValues1(myCol);
    }

    public static void PrintKeysAndValues1(StringDictionary myCol)
    {
      Console.WriteLine("   KEY         VALUE");
      foreach (DictionaryEntry d in myCol)
      {
        Console.WriteLine("   {0,-25} {1}", d.Key, d.Value);
      }
      Console.WriteLine();
    }

    public static void PrintKeysAndValues2(StringDictionary myCol)
    {
      IEnumerator myEnumerator = myCol.GetEnumerator();
      DictionaryEntry de;
      Console.WriteLine("  Key          VALUE");
      while (myEnumerator.MoveNext())
      {
        de = (DictionaryEntry)myEnumerator.Current;
        Console.WriteLine("   {0,-25} {1}", de.Key, de.Value);
      }
      Console.WriteLine();
    }

    public static void PrintKeysAndValues3(StringDictionary myCol)
    {

      string[] myKeys = new String[myCol.Count];
      myCol.Keys.CopyTo(myKeys, 0);
      Console.WriteLine("   INDEX KEY         VALUE");
      for (int i = 0; i < myCol.Count; i++)
      {
        Console.WriteLine("  {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] );
      }
      Console.WriteLine();
    }
  }
}
 

你可能感兴趣的:(C#)