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();
}
}
}
本文介绍了.NET Framework中System.Collections.Specialized命名空间下的StringDictionary类。详细解释了StringDictionary的应用场景,特别是配置文件管理和应用配置方面的作用,并提供了示例代码来展示如何进行增删查改等操作。
295

被折叠的 条评论
为什么被折叠?



