NameValueCollection-Klasse
Stellt eine Auflistung einander zugeordneter String-Schlüssel und String-Werte dar, auf die entweder über den Schlüssel oder über den Index zugegriffen werden kann.
Namespace: System.Collections.Specialized
Assembly: System (in system.dll)
Syntax
'Declaration
<SerializableAttribute> _
Public Class NameValueCollection
Inherits NameObjectCollectionBase
'Usage
Dim instance As NameValueCollection
[SerializableAttribute]
public class NameValueCollection : NameObjectCollectionBase
[SerializableAttribute]
public ref class NameValueCollection : public NameObjectCollectionBase
/** @attribute SerializableAttribute() */
public class NameValueCollection extends NameObjectCollectionBase
SerializableAttribute
public class NameValueCollection extends NameObjectCollectionBase
Hinweise
Diese Auflistung basiert auf der NameObjectCollectionBase-Klasse. Im Gegensatz zu NameObjectCollectionBase speichert diese Klasse mehrere Zeichenfolgenwerte unter einem einzigen Schlüssel.
Diese Klasse kann für Header, Abfragezeichenfolgen und Formulardaten verwendet werden.
Jedes Element ist ein Schlüssel-Wert-Paar.
Bei der Kapazität einer NameValueCollection handelt es sich um die Anzahl der Elemente, die die NameValueCollection enthalten kann. Die anfängliche Standardkapazität für eine NameValueCollection ist 0 (null). Beim Hinzufügen von Elementen zu einer NameValueCollection wird die Kapazität nach Bedarf automatisch durch Neureservierung erhöht.
Der Hashcodeanbieter verteilt Hashcodes für Schlüssel in der NameValueCollection. Der Hashcode-Standardanbieter ist CaseInsensitiveHashCodeProvider.
Der Comparer ermittelt, ob zwei Schlüssel gleich sind. Der Standardcomparer ist CaseInsensitiveComparer.
In .NET Framework, Version 1.0, verwendet diese Klasse kulturabhängige Zeichenfolgenvergleiche. In .NET Framework, Version 1.1 und höher, verwendet diese Klasse jedoch CultureInfo.InvariantCulture bei Zeichenfolgenvergleichen. Weitere Informationen über den Einfluss der Kultur auf Vergleiche und die Sortierung finden Sie unter Vergleichen und Sortieren von Daten für eine bestimmte KulturVergleichen und Sortieren von Daten für eine bestimmte Kultur und Durchführen kulturunabhängiger Zeichenfolgenoperationen.
NULL (Nothing in Visual Basic) ist als Schlüssel oder Wert zulässig.
Warnung
Die Get-Methode unterscheidet nicht zwischen einem Wert NULL (Nothing in Visual Basic), der zurückgegeben wird, weil der angegebene Schlüssel nicht gefunden wurde, und einem Wert NULL (Nothing in Visual Basic), der zurückgegeben wird, weil der dem Schlüssel zugeordnete Wert NULL (Nothing in Visual Basic) ist.
Thema | Position |
---|---|
Gewusst wie: Lesen von Anwendungseinstellungen aus der Datei Web.config | Konfigurieren von ASP.NET-Webanwendungen |
Gewusst wie: Lesen von Anwendungseinstellungen aus der Datei Web.config | Erstellen von ASP.NET-Webanwendungen in Visual Studio |
Gewusst wie: Lesen von Anwendungseinstellungen aus der Datei Web.config | Erstellen von ASP.NET-Webanwendungen in Visual Studio |
Beispiel
' The following code example demonstrates several of the properties and methods of ListDictionary.
Imports System
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 'Main
Public Shared Sub PrintKeysAndValues(myCol As NameValueCollection)
Dim myEnumerator As IEnumerator = myCol.GetEnumerator()
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 'PrintKeysAndValues
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 'PrintKeysAndValues2
End Class 'SamplesNameValueCollection
'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
'
'
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 ) {
IEnumerator myEnumerator = myCol.GetEnumerator();
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
*/
#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
*/
import System.*;
import System.Collections.*;
import System.Collections.Specialized.*;
public class SamplesNameValueCollection
{
public static void main(String[] args)
{
// 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.get_Item(1));
Console.WriteLine("Key \"red\" has the value {0}.",
myCol.get_Item("red"));
Console.WriteLine();
// Copies the values to a string array and displays the string array.
String myStrArr[] = new String[myCol.get_Count()];
myCol.CopyTo(myStrArr, 0);
Console.WriteLine("The string array contains:");
for (int iCtr = 0; iCtr < myStrArr.get_Length(); iCtr++) {
String s = myStrArr[iCtr];
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);
} //main
public static void PrintKeysAndValues(NameValueCollection myCol)
{
IEnumerator myEnumerator = myCol.GetEnumerator();
Console.WriteLine(" KEY VALUE");
for (int iCtr = 0; iCtr < myCol.get_Count(); iCtr++) {
String s = myCol.get_AllKeys()[iCtr];
Console.WriteLine(" {0,-10} {1}", s, myCol.get_Item(s));
}
Console.WriteLine();
} //PrintKeysAndValues
public static void PrintKeysAndValues2(NameValueCollection myCol)
{
Console.WriteLine(" [INDEX] KEY VALUE");
for (int i = 0; i < myCol.get_Count(); i++) {
Console.WriteLine(" [{0}] {1,-10} {2}",
System.Convert.ToString(i), System.Convert.ToString(
myCol.GetKey(i)), System.Convert.ToString(myCol.Get(i)));
}
Console.WriteLine();
} //PrintKeysAndValues2
} //SamplesNameValueCollection
/*
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
*/
Vererbungshierarchie
System.Object
System.Collections.Specialized.NameObjectCollectionBase
System.Collections.Specialized.NameValueCollection
System.Net.WebHeaderCollection
System.Web.HttpClientCertificate
Threadsicherheit
Öffentliche statische (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.
Diese Implementierung stellt keinen synchronisierten (threadsicheren) Wrapper für eine NameValueCollection bereit. Abgeleitete Klassen können jedoch mithilfe der SyncRoot-Eigenschaft der NameObjectCollectionBase-Klasse eigene synchronisierte Versionen der NameValueCollection erstellen.
Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert wird, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Um während der Enumeration Threadsicherheit zu gewährleisten, können Sie entweder die Auflistung während der gesamten Enumeration sperren, oder Sie können die durch andere Threads aufgrund von Änderungen ausgelösten Ausnahmen abfangen.
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
NameValueCollection-Member
System.Collections.Specialized-Namespace
NameObjectCollectionBase-Klasse