.NET C#: NameValueCollection

11516 ワード

NameValueCollection class is in System.Collection.Specialized assembly.
Unlike with HashTable, NameValueCollection can have more than one values for one key.
So there is no error for code below:
NameValueCollection myCol = new NameValueCollection();
myCol.Add("red", "rojo");
myCol.Add("red", "rouge");
while using HashTable there will be an error.
Let us see some demo:
  1 using System;

  2 using System.Collections;

  3 using System.Collections.Specialized;

  4 

  5 public class ConsoleTest

  6 {

  7 

  8     public static void Main()

  9     {

 10 

 11         // Creates and initializes a new NameValueCollection.

 12         NameValueCollection myCol = new NameValueCollection();

 13         myCol.Add("red", "rojo");

 14         myCol.Add("green", "verde");

 15         myCol.Add("blue", "azul");

 16         myCol.Add("red", "rouge");

 17 

 18         // Displays the values in the NameValueCollection in two different ways.

 19         Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:");

 20         PrintKeysAndValues(myCol);

 21         Console.WriteLine("Displays the elements using GetKey and Get:");

 22         PrintKeysAndValues2(myCol);

 23 

 24         // Gets a value either by index or by key.

 25         Console.WriteLine("Index 1 contains the value {0}.", myCol[1]);

 26         Console.WriteLine("Key \"red\" has the value {0}.", myCol["red"]);

 27         Console.WriteLine();

 28 

 29         // Copies the values to a string array and displays the string array.

 30         String[] myStrArr = new String[myCol.Count];

 31         myCol.CopyTo(myStrArr, 0);

 32         Console.WriteLine("The string array contains:");

 33         foreach (String s in myStrArr)

 34             Console.WriteLine("   {0}", s);

 35         Console.WriteLine();

 36 

 37         // Searches for a key and deletes it.

 38         myCol.Remove("green");

 39         Console.WriteLine("The collection contains the following elements after removing \"green\":");

 40         PrintKeysAndValues(myCol);

 41 

 42         // Clears the entire collection.

 43         myCol.Clear();

 44         Console.WriteLine("The collection contains the following elements after it is cleared:");

 45         PrintKeysAndValues(myCol);

 46 

 47     }

 48 

 49     public static void PrintKeysAndValues(NameValueCollection myCol)

 50     {

 51         Console.WriteLine("   KEY        VALUE");

 52         foreach (String s in myCol.AllKeys)

 53             Console.WriteLine("   {0,-10} {1}", s, myCol[s]);

 54         Console.WriteLine();

 55     }

 56 

 57     public static void PrintKeysAndValues2(NameValueCollection myCol)

 58     {

 59         Console.WriteLine("   [INDEX] KEY        VALUE");

 60         for (int i = 0; i < myCol.Count; i++)

 61             Console.WriteLine("   [{0}]     {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i));

 62         Console.WriteLine();

 63     }

 64 

 65 

 66 }

 67 

 68 /*

 69 

 70 This code produces the following output.

 71 

 72 Displays the elements using the AllKeys property and the Item (indexer) property:

 73    KEY        VALUE

 74    red        rojo,rouge

 75    green      verde

 76    blue       azul

 77 

 78 Displays the elements using GetKey and Get:

 79    [INDEX] KEY        VALUE

 80    [0]     red        rojo,rouge

 81    [1]     green      verde

 82    [2]     blue       azul

 83 

 84 Index 1 contains the value verde.

 85 Key "red" has the value rojo,rouge.

 86 

 87 The string array contains:

 88    rojo,rouge

 89    verde

 90    azul

 91 

 92 The collection contains the following elements after removing "green":

 93    KEY        VALUE

 94    red        rojo,rouge

 95    blue       azul

 96 

 97 The collection contains the following elements after it is cleared:

 98    KEY        VALUE

 99 

100 

101 */

reference: https://msdn.microsoft.com/zh-cn/library/system.collections.specialized.namevaluecollection.aspx