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,這個類別可以在單一索引鍵下儲存多個字串值。

這個類別可用於標頭、查詢字串和表單數據。

此類型的集合不會保留專案的排序,而且列舉集合時不保證有特定的順序。

NameValueCollection 容量是 可以保留的項目 NameValueCollection 數目。 隨著新增元素,其容量會透過重新配置自動增加。

哈希碼提供者會為 中的 NameValueCollection索引鍵分配哈希碼。 預設哈希碼提供者為 CaseInsensitiveHashCodeProvider

比較子會判斷兩個索引鍵是否相等。 默認比較子是 CaseInsensitiveComparer 使用 不因文化特性而異慣例的 ,也就是說,索引鍵比較預設不區分大小寫。 若要執行區分大小寫的索引鍵比較,請呼叫NameValueCollection.NameValueCollection(IEqualityComparer)建構函式,並提供、 StringComparer.InvariantCultureStringComparer.Ordinal 的值StringComparer.CurrentCulture做為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函式,但衍生類別可以使用 類別的 NameObjectCollectionBase 屬性,建立自己的同步版本NameValueCollectionSyncRoot

透過集合列舉本質上不是安全線程程式。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

另請參閱