NameValueCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
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
- 繼承
- 衍生
- 屬性
範例
#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函式,但衍生類別可以使用 類別的 NameObjectCollectionBase 屬性,建立自己的同步版本NameValueCollectionSyncRoot。
透過集合列舉本質上不是安全線程程式。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。