NameValueCollection 類別

定義

表示相關 String 索引鍵和 String 值的集合,而這些可以利用索引鍵或索引來存取。Represents a collection of associated String keys and String values that can be accessed either with the key or with the index.

public ref class NameValueCollection : System::Collections::Specialized::NameObjectCollectionBase
[System.Serializable]
public class NameValueCollection : System.Collections.Specialized.NameObjectCollectionBase
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類別為基礎。This collection is based on the NameObjectCollectionBase class. 集合中的每個元素都是索引鍵/值組。Each element of the collection is a key/value pair. 不過,與不同NameObjectCollectionBase的是,這個類別可以在單一索引鍵下儲存多個字串值。However, unlike the NameObjectCollectionBase, this class can store multiple string values under a single key.

此類別可用於標頭、查詢字串和表單資料。This class can be used for headers, query strings and form data.

此類型的集合不會保留專案的順序,也不會在列舉集合時保證特定的順序。Collections of this type do not preserve the ordering of elements, and no particular ordering is guaranteed when enumerating the collection.

的容量NameValueCollectionNameValueCollection可以保存的元素數目。The capacity of a NameValueCollection is the number of elements the NameValueCollection can hold. 新增專案時,其容量會視需要透過重新配置而自動增加。As elements are added, its capacity is automatically increased as required through reallocation.

雜湊程式碼提供者會不表達中NameValueCollection索引鍵的雜湊碼。The hash code provider dispenses hash codes for keys in the NameValueCollection. 預設雜湊程式碼提供者CaseInsensitiveHashCodeProvider為。The default hash code provider is the CaseInsensitiveHashCodeProvider.

比較子會判斷兩個索引鍵是否相等。The comparer determines whether two keys are equal. 預設比較子CaseInsensitiveComparer是使用不因文化特性而異的慣例的,也就是說,索引鍵比較預設不區分大小寫。The default comparer is a CaseInsensitiveComparer that uses the conventions of the invariant culture; that is, key comparisons are case-insensitive by default. 若要執行區分大小寫的索引鍵比較NameValueCollection.NameValueCollection(IEqualityComparer) ,請呼叫此函式, StringComparer.CurrentCultureStringComparer.InvariantCulture提供equalityComparerStringComparer.Ordinal或的值做為引數。To perform case-sensitive key comparisons, call the NameValueCollection.NameValueCollection(IEqualityComparer) constructor, and provide a value of StringComparer.CurrentCulture, StringComparer.InvariantCulture, or StringComparer.Ordinal as the equalityComparer argument. 如需文化特性如何影響比較和排序的詳細資訊,請參閱執行不區分文化特性的字串作業For more information about how culture affects comparisons and sorting, see Performing Culture-Insensitive String Operations.

null允許做為索引鍵或值。null is allowed as a key or as a value.

警告

方法不會null區分傳回的,因為找不到指定的索引鍵,而且null會傳回,因為與索引鍵相關聯的值null是。 GetThe Get method does not distinguish between null which is returned because the specified key is not found and null which is returned because the value associated with the key is null.

建構函式

NameValueCollection()

初始化 NameValueCollection 類別的新執行個體,這個執行個體是空白的、有預設的初始容量,並採用預設的不區分大小寫的雜湊程式碼提供者和預設的不區分大小寫的比較子 (Comparer)。Initializes a new instance of the NameValueCollection class that is empty, has the default initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.

NameValueCollection(IEqualityComparer)

初始化 NameValueCollection 類別的新執行個體,這個執行個體是空白的、具有預設的初始容量,並使用指定的 IEqualityComparer 物件。Initializes a new instance of the NameValueCollection class that is empty, has the default initial capacity, and uses the specified IEqualityComparer object.

NameValueCollection(IHashCodeProvider, IComparer)

初始化 NameValueCollection 類別的新執行個體,這個執行個體是空白的、有預設的初始容量,並採用指定的雜湊程式碼提供者和指定的比較子。Initializes a new instance of the NameValueCollection class that is empty, has the default initial capacity and uses the specified hash code provider and the specified comparer.

NameValueCollection(Int32)

初始化 NameValueCollection 類別的新執行個體,這個執行個體是空白的、有指定的初始容量,並採用預設的不區分大小寫的雜湊程式碼提供者和預設的不區分大小寫的比較子。Initializes a new instance of the NameValueCollection class that is empty, has the specified initial capacity and uses the default case-insensitive hash code provider and the default case-insensitive comparer.

NameValueCollection(Int32, IEqualityComparer)

初始化 NameValueCollection 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用指定的 IEqualityComparer 物件。Initializes a new instance of the NameValueCollection class that is empty, has the specified initial capacity, and uses the specified IEqualityComparer object.

NameValueCollection(Int32, IHashCodeProvider, IComparer)

初始化 NameValueCollection 類別的新執行個體,這個執行個體是空白的、具有指定的初始容量,並使用指定的雜湊碼提供者和指定的比較子。Initializes a new instance of the NameValueCollection class that is empty, has the specified initial capacity and uses the specified hash code provider and the specified comparer.

NameValueCollection(Int32, NameValueCollection)

從指定的 NameValueCollection 複製項目至新的 NameValueCollection,使其具有指定的初始容量,或是與複製的項目數目一樣的初始容量,端視何者較大,並使用預設的不區分大小寫的雜湊程式碼提供者,以及預設的不區分大小寫的比較子。Copies the entries from the specified NameValueCollection to a new NameValueCollection with the specified initial capacity or the same initial capacity as the number of entries copied, whichever is greater, and using the default case-insensitive hash code provider and the default case-insensitive comparer.

NameValueCollection(NameValueCollection)

從指定的 NameValueCollection 複製項目至新的 NameValueCollection,使其具有與複製的項目數目一樣的初始容量,並使用與來源集合相同的雜湊程式碼提供者,以及相同的比較子。Copies the entries from the specified NameValueCollection to a new NameValueCollection with the same initial capacity as the number of entries copied and using the same hash code provider and the same comparer as the source collection.

NameValueCollection(SerializationInfo, StreamingContext)

初始化 NameValueCollection 類別的新執行個體,這個執行個體可序列化,並使用指定的 SerializationInfoStreamingContextInitializes a new instance of the NameValueCollection class that is serializable and uses the specified SerializationInfo and StreamingContext.

屬性

AllKeys

取得 NameValueCollection 中的所有索引鍵。Gets all the keys in the NameValueCollection.

Count

取得 NameObjectCollectionBase 執行個體中包含的索引鍵/值組數目。Gets the number of key/value pairs contained in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
IsReadOnly

取得或設定值,表示 NameObjectCollectionBase 執行個體是否為唯讀。Gets or sets a value indicating whether the NameObjectCollectionBase instance is read-only.

(繼承來源 NameObjectCollectionBase)
Item[Int32]

取得 NameValueCollection 的指定索引的項目。Gets the entry at the specified index of the NameValueCollection.

Item[String]

使用 NameValueCollection 中指定的索引鍵來取得或設定項目。Gets or sets the entry with the specified key in the NameValueCollection.

Keys

取得 NameObjectCollectionBase.KeysCollection 執行個體,其中包含 NameObjectCollectionBase 執行個體中的所有索引鍵。Gets a NameObjectCollectionBase.KeysCollection instance that contains all the keys in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)

方法

Add(NameValueCollection)

將指定 NameValueCollection 中的項目複製到目前的 NameValueCollectionCopies the entries in the specified NameValueCollection to the current NameValueCollection.

Add(String, String)

將具有指定名稱和數值的項目加入 NameValueCollectionAdds an entry with the specified name and value to the NameValueCollection.

BaseAdd(String, Object)

將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。Adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseClear()

將所有項目從 NameObjectCollectionBase 執行個體中移除。Removes all entries from the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGet(Int32)

取得 NameObjectCollectionBase 執行個體指定索引處之項目的值。Gets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGet(String)

NameObjectCollectionBase 執行個體取得具有指定索引鍵之第一個項目的值。Gets the value of the first entry with the specified key from the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGetAllKeys()

傳回 String 陣列,其中包含 NameObjectCollectionBase 執行個體中的所有索引鍵。Returns a String array that contains all the keys in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGetAllValues()

傳回 Object 陣列,其中包含 NameObjectCollectionBase 執行個體中的所有值。Returns an Object array that contains all the values in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGetAllValues(Type)

傳回指定類型的陣列,其中包含 NameObjectCollectionBase 執行個體中的所有值。Returns an array of the specified type that contains all the values in the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseGetKey(Int32)

取得 NameObjectCollectionBase 執行個體指定索引處之項目的索引鍵。Gets the key of the entry at the specified index of the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseHasKeys()

取得值,表示 NameObjectCollectionBase 執行個體是否包含其索引鍵不是 null 的項目。Gets a value indicating whether the NameObjectCollectionBase instance contains entries whose keys are not null.

(繼承來源 NameObjectCollectionBase)
BaseRemove(String)

將具有指定索引鍵的項目從 NameObjectCollectionBase 中移除。Removes the entries with the specified key from the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseRemoveAt(Int32)

移除 NameObjectCollectionBase 執行個體指定索引處的項目。Removes the entry at the specified index of the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseSet(Int32, Object)

設定 NameObjectCollectionBase 執行個體指定索引處之項目的值。Sets the value of the entry at the specified index of the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
BaseSet(String, Object)

設定 NameObjectCollectionBase 執行個體中具有指定索引鍵之第一個項目的值 (如果有找到),否則將具有指定索引鍵和值的項目加入 NameObjectCollectionBase 執行個體。Sets the value of the first entry with the specified key in the NameObjectCollectionBase instance, if found; otherwise, adds an entry with the specified key and value into the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
Clear()

使快取陣列失效,並移除 NameValueCollection 中的所有項目。Invalidates the cached arrays and removes all entries from the NameValueCollection.

CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 NameValueCollection 複製到相容的一維 ArrayCopies the entire NameValueCollection to a compatible one-dimensional Array, starting at the specified index of the target array.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
Get(Int32)

取得組合成一個逗號分隔清單的 NameValueCollection 的指定索引值。Gets the values at the specified index of the NameValueCollection combined into one comma-separated list.

Get(String)

由組合成一個逗號分隔清單的 NameValueCollection 取得與指定索引鍵相關的值。Gets the values associated with the specified key from the NameValueCollection combined into one comma-separated list.

GetEnumerator()

傳回在 NameObjectCollectionBase 中逐一查看的列舉值。Returns an enumerator that iterates through the NameObjectCollectionBase.

(繼承來源 NameObjectCollectionBase)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetKey(Int32)

取得 NameValueCollection 的指定索引的索引鍵。Gets the key at the specified index of the NameValueCollection.

GetObjectData(SerializationInfo, StreamingContext)

實作 ISerializable 介面,並傳回序列化 NameObjectCollectionBase 執行個體所需的資料。Implements the ISerializable interface and returns the data needed to serialize the NameObjectCollectionBase instance.

(繼承來源 NameObjectCollectionBase)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
GetValues(Int32)

取得 NameValueCollection 的指定索引的值。Gets the values at the specified index of the NameValueCollection.

GetValues(String)

NameValueCollection 取得與指定索引鍵關聯的值。Gets the values associated with the specified key from the NameValueCollection.

HasKeys()

取得值,指出 NameValueCollection 是否包含非 null 的索引鍵。Gets a value indicating whether the NameValueCollection contains keys that are not null.

InvalidateCachedArrays()

將集合的快取陣列重設為 nullResets the cached arrays of the collection to null.

MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
OnDeserialization(Object)

實作 ISerializable 介面,並於還原序列化完成時引發還原序列化事件。Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.

(繼承來源 NameObjectCollectionBase)
Remove(String)

將具有指定索引鍵的項目從 NameObjectCollectionBase 中移除。Removes the entries with the specified key from the NameObjectCollectionBase instance.

Set(String, String)

設定 NameValueCollection 中項目的值。Sets the value of an entry in the NameValueCollection.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

明確介面實作

ICollection.CopyTo(Array, Int32)

從目標陣列的指定索引開始,將整個 NameObjectCollectionBase 複製到相容的一維 ArrayCopies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(繼承來源 NameObjectCollectionBase)
ICollection.IsSynchronized

取得值,表示是否要同步處理 (執行緒安全) 對 NameObjectCollectionBase 物件的存取。Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).

(繼承來源 NameObjectCollectionBase)
ICollection.SyncRoot

取得可用來同步處理對 NameObjectCollectionBase 物件之存取的物件。Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.

(繼承來源 NameObjectCollectionBase)

擴充方法

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於

執行緒安全性

此類型的Shared公用靜態(在 Visual Basic 中)成員是安全線程。Public static (Shared in Visual Basic) members of this type are thread safe. 不保證任何執行個體成員是安全執行緒。Any instance members are not guaranteed to be thread safe.

這個實作為不提供的同步處理(安全線程NameValueCollection)包裝函式,但衍生的類別可以NameObjectCollectionBase使用SyncRoot類別的屬性, NameValueCollection建立自己的同步處理版本。This implementation does not provide a synchronized (thread safe) wrapper for a NameValueCollection, but derived classes can create their own synchronized versions of the NameValueCollection using the SyncRoot property of the NameObjectCollectionBase class.

透過集合進行列舉,本質上並不是安全線程的程式。Enumerating through a collection is intrinsically not a thread safe procedure. 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

另請參閱