NameValueCollection 类

定义

表示可通过键或索引访问的关联 String 键和 String 值的集合。

public ref class NameValueCollection : System::Collections::Specialized::NameObjectCollectionBase
public class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase
[System.Serializable]
public class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase
type NameValueCollection = class
    inherit NameObjectCollectionBase
[<System.Serializable>]
type NameValueCollection = class
    inherit NameObjectCollectionBase
Public Class NameValueCollection
Inherits NameObjectCollectionBase
继承
NameValueCollection
派生
属性

示例

#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,此类可以在单个键下存储多个字符串值。

此类可用于标头、查询字符串和表单数据。

此类型的集合不会保留元素的排序,并且枚举集合时不会保证特定排序。

a NameValueCollection 的容量是可以保留的元素 NameValueCollection 数。 添加元素时,通过重新分配自动增加其容量。

哈希代码提供程序为密钥 NameValueCollection分配哈希代码。 默认哈希代码提供程序为 CaseInsensitiveHashCodeProvider.

比较器确定两个键是否相等。 默认比较器是 CaseInsensitiveComparer 使用 固定区域性的约定;也就是说,默认情况下,关键比较不区分大小写。 若要执行区分大小写的键比较,请调用 NameValueCollection.NameValueCollection(IEqualityComparer) 构造函数并提供值 StringComparer.CurrentCultureStringComparer.InvariantCultureStringComparer.Ordinal 作为 equalityComparer 参数。 有关区域性如何影响比较和排序的详细信息,请参阅 执行Culture-Insensitive字符串操作

null 允许作为键或值。

注意

该方法 Get 不区分返回哪个项, null 因为找不到指定的键,而 null 返回的键是与键 null关联的值。

构造函数

NameValueCollection()

初始化 NameValueCollection 类的新实例,该实例为空且具有默认初始容量,并使用不区分大小写的默认哈希代码提供程序和不区分大小写的默认比较器。

NameValueCollection(IEqualityComparer)

初始化 NameValueCollection 类的新实例,该实例为空、具有默认的初始容量并使用指定的 IEqualityComparer 对象。

NameValueCollection(IHashCodeProvider, IComparer)
已过时。
已过时。

初始化 NameValueCollection 类的新实例,该实例为空且具有默认初始容量,并使用指定的哈希代码提供程序和指定的比较器。

NameValueCollection(Int32)

初始化 NameValueCollection 类的新实例,该实例为空且具有指定的初始容量,并使用不区分大小写的默认哈希代码提供程序和不区分大小写的默认比较器。

NameValueCollection(Int32, IEqualityComparer)

初始化 NameValueCollection 类的新实例,该实例为空、具有指定的初始容量并使用指定的 IEqualityComparer 对象。

NameValueCollection(Int32, IHashCodeProvider, IComparer)
已过时。
已过时。

初始化 NameValueCollection 类的新实例,该实例为空且具有指定的初始容量,并使用指定的哈希代码提供程序和指定的比较器。

NameValueCollection(Int32, NameValueCollection)

将项从指定的 NameValueCollection 复制到一个新的 NameValueCollection,这个新集合使用指定的初始容量或与具有与复制的项数相等的初始容量(两者中较大的一个),并使用不区分大小写的默认哈希代码提供程序和不区分大小写的默认比较器。

NameValueCollection(NameValueCollection)

将项从指定的 NameValueCollection 复制到一个新的 NameValueCollection,这个新集合的初始容量与复制的项数相等,并使用与源集合相同的哈希代码提供程序和比较器。

NameValueCollection(SerializationInfo, StreamingContext)

初始化 NameValueCollection 类的新实例,该实例可序列化且使用指定的 SerializationInfoStreamingContext

属性

AllKeys

获取 NameValueCollection 中的所有键。

Count

获取包含在 NameObjectCollectionBase 实例中的键/值对的数目。

(继承自 NameObjectCollectionBase)
IsReadOnly

获取或设置一个值,通过该值指示 NameObjectCollectionBase 实例是否为只读的。

(继承自 NameObjectCollectionBase)
Item[Int32]

获取 NameValueCollection 中指定索引处的项。

Item[String]

获取或设置 NameValueCollection 中具有指定键的项。

Keys

获取包含 NameObjectCollectionBase.KeysCollection 实例中所有键的 NameObjectCollectionBase 实例。

(继承自 NameObjectCollectionBase)

方法

Add(NameValueCollection)

将指定 NameValueCollection 中的项复制到当前 NameValueCollection

Add(String, String)

将具有指定名称和值的项添加到 NameValueCollection

BaseAdd(String, Object)

将具有指定键和值的项添加到 NameObjectCollectionBase 实例中。

(继承自 NameObjectCollectionBase)
BaseClear()

移除 NameObjectCollectionBase 实例中的所有项。

(继承自 NameObjectCollectionBase)
BaseGet(Int32)

获取 NameObjectCollectionBase 实例的指定索引处的项值。

(继承自 NameObjectCollectionBase)
BaseGet(String)

获取 NameObjectCollectionBase 实例中第一个具有指定键的项值。

(继承自 NameObjectCollectionBase)
BaseGetAllKeys()

返回 String 数组,该数组包含 NameObjectCollectionBase 实例中的所有键。

(继承自 NameObjectCollectionBase)
BaseGetAllValues()

返回 Object 数组,该数组包含 NameObjectCollectionBase 实例中的所有值。

(继承自 NameObjectCollectionBase)
BaseGetAllValues(Type)

返回指定类型的数组,该数组包含 NameObjectCollectionBase 实例中的所有值。

(继承自 NameObjectCollectionBase)
BaseGetKey(Int32)

获取 NameObjectCollectionBase 实例的指定索引处的项键。

(继承自 NameObjectCollectionBase)
BaseHasKeys()

获取一个值,通过该值指示 NameObjectCollectionBase 实例是否包含键不为 null 的项。

(继承自 NameObjectCollectionBase)
BaseRemove(String)

移除 NameObjectCollectionBase 实例中具有指定键的项。

(继承自 NameObjectCollectionBase)
BaseRemoveAt(Int32)

移除 NameObjectCollectionBase 实例的指定索引处的项。

(继承自 NameObjectCollectionBase)
BaseSet(Int32, Object)

设置 NameObjectCollectionBase 实例的指定索引处的项值。

(继承自 NameObjectCollectionBase)
BaseSet(String, Object)

NameObjectCollectionBase 实例中第一个具有指定键的项设置值(如果有这样的项);否则将具有指定键和值的项添加到 NameObjectCollectionBase 实例中。

(继承自 NameObjectCollectionBase)
Clear()

使缓存数组无效,并将所有项从 NameValueCollection 中移除。

CopyTo(Array, Int32)

从目标数组的指定索引处开始将整个 NameValueCollection 复制到兼容的一维 Array

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Get(Int32)

获取 NameValueCollection 中指定索引处的值,这些值已合并为一个以逗号分隔的列表。

Get(String)

获取与 NameValueCollection 中的指定键关联的值,这些值已合并为一个以逗号分隔的列表。

GetEnumerator()

返回循环访问 NameObjectCollectionBase 的枚举数。

(继承自 NameObjectCollectionBase)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetKey(Int32)

获取 NameValueCollection 的指定索引处的键。

GetObjectData(SerializationInfo, StreamingContext)

实现 ISerializable 接口,并返回序列化 NameObjectCollectionBase 实例所需的数据。

(继承自 NameObjectCollectionBase)
GetType()

获取当前实例的 Type

(继承自 Object)
GetValues(Int32)

获取 NameValueCollection 中指定索引处的值。

GetValues(String)

获取与 NameValueCollection 中的指定键关联的值。

HasKeys()

获取一个值,该值指示 NameValueCollection 是否包含非 null 的键。

InvalidateCachedArrays()

将集合的缓存数组重置为 null

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
OnDeserialization(Object)

实现 ISerializable 接口,并在完成反序列化之后引发反序列化事件。

(继承自 NameObjectCollectionBase)
Remove(String)

移除 NameObjectCollectionBase 实例中具有指定键的项。

Set(String, String)

设置 NameValueCollection 中某个项的值。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

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但派生类可以创建其自己的同步版本的 NameValueCollection 使用 SyncRoot 类的属性 NameObjectCollectionBase

通过集合进行枚举本质上不是线程安全过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。

另请参阅