NameValueCollection 类  
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
public ref class NameValueCollection : System::Collections::Specialized::NameObjectCollectionBasepublic class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase[System.Serializable]
public class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBasetype NameValueCollection = class
    inherit NameObjectCollectionBase[<System.Serializable>]
type NameValueCollection = class
    inherit NameObjectCollectionBasePublic Class NameValueCollection
Inherits NameObjectCollectionBase- 继承
- 派生
- 属性
示例
#using <System.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;
void PrintKeysAndValues( NameValueCollection^ myCol );
void PrintKeysAndValues2( NameValueCollection^ myCol );
int main()
{
   // Creates and initializes a new NameValueCollection.
   NameValueCollection^ myCol = gcnew NameValueCollection;
   myCol->Add( "red", "rojo" );
   myCol->Add( "green", "verde" );
   myCol->Add( "blue", "azul" );
   myCol->Add( "red", "rouge" );
   // Displays the values in the NameValueCollection in two different ways.
   Console::WriteLine( "Displays the elements using the AllKeys property and the Item (indexer) property:" );
   PrintKeysAndValues( myCol );
   Console::WriteLine( "Displays the elements using GetKey and Get:" );
   PrintKeysAndValues2( myCol );
   // Gets a value either by index or by key.
   Console::WriteLine( "Index 1 contains the value {0}.", myCol[ 1 ] );
   Console::WriteLine( "Key \"red\" has the value {0}.", myCol[ "red" ] );
   Console::WriteLine();
   // Copies the values to a string array and displays the string array.
   array<String^>^myStrArr = gcnew array<String^>(myCol->Count);
   myCol->CopyTo( myStrArr, 0 );
   Console::WriteLine( "The string array contains:" );
   for each ( String^ s in myStrArr )
      Console::WriteLine( "   {0}", s );
   Console::WriteLine();
   // Searches for a key and deletes it.
   myCol->Remove( "green" );
   Console::WriteLine( "The collection contains the following elements after removing \"green\":" );
   PrintKeysAndValues( myCol );
   // Clears the entire collection.
   myCol->Clear();
   Console::WriteLine( "The collection contains the following elements after it is cleared:" );
   PrintKeysAndValues( myCol );
}
void PrintKeysAndValues( NameValueCollection^ myCol )
{
   Console::WriteLine( "   KEY        VALUE" );
   for each ( String^ s in myCol->AllKeys )
      Console::WriteLine( "   {0,-10} {1}", s, myCol[s] );
   Console::WriteLine();
}
void PrintKeysAndValues2( NameValueCollection^ myCol )
{
   Console::WriteLine( "   [INDEX] KEY        VALUE" );
   for ( int i = 0; i < myCol->Count; i++ )
      Console::WriteLine( "   [{0}]     {1,-10} {2}", i, myCol->GetKey( i ), myCol->Get( i ) );
   Console::WriteLine();
}
/*
This code produces the following output.
Displays the elements using the AllKeys property and the Item (indexer) property:
   KEY        VALUE
   red        rojo,rouge
   green      verde
   blue       azul
Displays the elements using GetKey and Get:
   [INDEX] KEY        VALUE
   [0]     red        rojo,rouge
   [1]     green      verde
   [2]     blue       azul
Index 1 contains the value verde.
Key "red" has the value rojo,rouge.
The string array contains:
   rojo,rouge
   verde
   azul
The collection contains the following elements after removing "green":
   KEY        VALUE
   red        rojo,rouge
   blue       azul
The collection contains the following elements after it is cleared:
   KEY        VALUE
*/
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesNameValueCollection  {
   public static void Main()  {
      // Creates and initializes a new NameValueCollection.
      NameValueCollection myCol = new NameValueCollection();
      myCol.Add( "red", "rojo" );
      myCol.Add( "green", "verde" );
      myCol.Add( "blue", "azul" );
      myCol.Add( "red", "rouge" );
      // Displays the values in the NameValueCollection in two different ways.
      Console.WriteLine( "Displays the elements using the AllKeys property and the Item (indexer) property:" );
      PrintKeysAndValues( myCol );
      Console.WriteLine( "Displays the elements using GetKey and Get:" );
      PrintKeysAndValues2( myCol );
      // Gets a value either by index or by key.
      Console.WriteLine( "Index 1 contains the value {0}.", myCol[1] );
      Console.WriteLine( "Key \"red\" has the value {0}.", myCol["red"] );
      Console.WriteLine();
      // Copies the values to a string array and displays the string array.
      String[] myStrArr = new String[myCol.Count];
      myCol.CopyTo( myStrArr, 0 );
      Console.WriteLine( "The string array contains:" );
      foreach ( String s in myStrArr )
         Console.WriteLine( "   {0}", s );
      Console.WriteLine();
      // Searches for a key and deletes it.
      myCol.Remove( "green" );
      Console.WriteLine( "The collection contains the following elements after removing \"green\":" );
      PrintKeysAndValues( myCol );
      // Clears the entire collection.
      myCol.Clear();
      Console.WriteLine( "The collection contains the following elements after it is cleared:" );
      PrintKeysAndValues( myCol );
   }
   public static void PrintKeysAndValues( NameValueCollection myCol )  {
      Console.WriteLine( "   KEY        VALUE" );
      foreach ( String s in myCol.AllKeys )
         Console.WriteLine( "   {0,-10} {1}", s, myCol[s] );
      Console.WriteLine();
   }
   public static void PrintKeysAndValues2( NameValueCollection myCol )  {
      Console.WriteLine( "   [INDEX] KEY        VALUE" );
      for ( int i = 0; i < myCol.Count; i++ )
         Console.WriteLine( "   [{0}]     {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i) );
      Console.WriteLine();
   }
}
/*
This code produces the following output.
Displays the elements using the AllKeys property and the Item (indexer) property:
   KEY        VALUE
   red        rojo,rouge
   green      verde
   blue       azul
Displays the elements using GetKey and Get:
   [INDEX] KEY        VALUE
   [0]     red        rojo,rouge
   [1]     green      verde
   [2]     blue       azul
Index 1 contains the value verde.
Key "red" has the value rojo,rouge.
The string array contains:
   rojo,rouge
   verde
   azul
The collection contains the following elements after removing "green":
   KEY        VALUE
   red        rojo,rouge
   blue       azul
The collection contains the following elements after it is cleared:
   KEY        VALUE
*/
' The following code example demonstrates several of the properties and methods of ListDictionary.
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesNameValueCollection
    Public Shared Sub Main()
        ' Creates and initializes a new NameValueCollection.
        Dim myCol As New NameValueCollection()
        myCol.Add("red", "rojo")
        myCol.Add("green", "verde")
        myCol.Add("blue", "azul")
        myCol.Add("red", "rouge")
        ' Displays the values in the NameValueCollection in two different ways.
        Console.WriteLine("Displays the elements using the AllKeys property and the Item (indexer) property:")
        PrintKeysAndValues(myCol)
        Console.WriteLine("Displays the elements using GetKey and Get:")
        PrintKeysAndValues2(myCol)
        ' Gets a value either by index or by key.
        Console.WriteLine("Index 1 contains the value {0}.", myCol(1))
        Console.WriteLine("Key ""red"" has the value {0}.", myCol("red"))
        Console.WriteLine()
        ' Copies the values to a string array and displays the string array.
        Dim myStrArr(myCol.Count) As String
        myCol.CopyTo(myStrArr, 0)
        Console.WriteLine("The string array contains:")
        Dim s As String
        For Each s In myStrArr
            Console.WriteLine("   {0}", s)
        Next s
        Console.WriteLine()
        ' Searches for a key and deletes it.
        myCol.Remove("green")
        Console.WriteLine("The collection contains the following elements after removing ""green"":")
        PrintKeysAndValues(myCol)
        ' Clears the entire collection.
        myCol.Clear()
        Console.WriteLine("The collection contains the following elements after it is cleared:")
        PrintKeysAndValues(myCol)
    End Sub
    Public Shared Sub PrintKeysAndValues(myCol As NameValueCollection)
        Console.WriteLine("   KEY        VALUE")
        Dim s As String
        For Each s In  myCol.AllKeys
            Console.WriteLine("   {0,-10} {1}", s, myCol(s))
        Next s
        Console.WriteLine()
    End Sub
    Public Shared Sub PrintKeysAndValues2(myCol As NameValueCollection)
        Console.WriteLine("   [INDEX] KEY        VALUE")
        Dim i As Integer
        For i = 0 To myCol.Count - 1
            Console.WriteLine("   [{0}]     {1,-10} {2}", i, myCol.GetKey(i), myCol.Get(i))
        Next i
        Console.WriteLine()
    End Sub
End Class
'This code produces the following output.
'
'Displays the elements using the AllKeys property and the Item (indexer) property:
'   KEY        VALUE
'   red        rojo,rouge
'   green      verde
'   blue       azul
'
'Displays the elements using GetKey and Get:
'   [INDEX] KEY        VALUE
'   [0]     red        rojo,rouge
'   [1]     green      verde
'   [2]     blue       azul
'
'Index 1 contains the value verde.
'Key "red" has the value rojo,rouge.
'
'The string array contains:
'   red
'   green
'   blue
'
'
'The collection contains the following elements after removing "green":
'   KEY        VALUE
'   red        rojo,rouge
'   blue       azul
'
'The collection contains the following elements after it is cleared:
'   KEY        VALUE
'
'
注解
此集合基于 NameObjectCollectionBase 类。 集合的每个元素都是一个键/值对。 但是,与 不同, NameObjectCollectionBase此类可以在单个键下存储多个字符串值。
此类可用于标头、查询字符串和表单数据。
此类型的集合不保留元素的顺序,枚举集合时不保证特定的排序。
的 NameValueCollection 容量是 可以容纳的元素 NameValueCollection 数。 添加元素时,会根据需要通过重新分配自动增加其容量。
哈希代码提供程序为 中的 NameValueCollection键分配哈希代码。 默认哈希代码提供程序为 CaseInsensitiveHashCodeProvider。
比较器确定两个键是否相等。 默认比较器是使用CaseInsensitiveComparer固定区域性约定的 ;也就是说,默认情况下,键比较不区分大小写。 若要执行区分大小写的键比较,请调用 NameValueCollection.NameValueCollection(IEqualityComparer) 构造函数,并提供 、 StringComparer.InvariantCulture或 StringComparer.Ordinal 的值StringComparer.CurrentCulture作为equalityComparer参数。 有关区域性如何影响比较和排序的详细信息,请参阅 执行 Culture-Insensitive 字符串操作。
              null 允许作为键或值。
注意
方法 Get 不区分 null 因找不到指定的键而返回的和 null 返回的,因为与键 null关联的值为 。
构造函数
属性
| AllKeys | 获取 NameValueCollection 中的所有键。 | 
| Count | 获取包含在 NameObjectCollectionBase 实例中的键/值对的数目。(继承自 NameObjectCollectionBase) | 
| IsReadOnly | 获取或设置一个值,通过该值指示 NameObjectCollectionBase 实例是否为只读的。(继承自 NameObjectCollectionBase) | 
| Item[Int32] | 获取 NameValueCollection 中指定索引处的项。 | 
| Item[String] | 获取或设置 NameValueCollection 中具有指定键的项。 | 
| Keys | 获取包含 NameObjectCollectionBase.KeysCollection 实例中所有键的 NameObjectCollectionBase 实例。(继承自 NameObjectCollectionBase) | 
方法
显式接口实现
| ICollection.CopyTo(Array, Int32) | 从目标数组的指定索引处开始将整个 NameObjectCollectionBase 复制到兼容的一维 Array。(继承自 NameObjectCollectionBase) | 
| ICollection.IsSynchronized | 获取一个值,该值指示对 NameObjectCollectionBase 对象的访问是否同步(线程安全)。(继承自 NameObjectCollectionBase) | 
| ICollection.SyncRoot | 获取一个对象,该对象可用于同步对 NameObjectCollectionBase 对象的访问。(继承自 NameObjectCollectionBase) | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 | 
适用于
线程安全性
Visual Basic 中的公共静态 (Shared) 此类型的成员是线程安全的。 但不保证所有实例成员都是线程安全的。
此实现不会为 NameValueCollection提供同步 (线程安全的) 包装器,但派生类可以使用 类的 NameValueCollectionSyncRootNameObjectCollectionBase 属性创建自己的同步版本的 。
通过集合枚举本质上不是线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。