Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Fügt dem ListDictionary einen Eintrag mit dem angegebenen Schlüssel und Wert hinzu.
Namespace: System.Collections.Specialized
Assembly: System (in system.dll)
Syntax
'Declaration
Public Sub Add ( _
key As Object, _
value As Object _
)
'Usage
Dim instance As ListDictionary
Dim key As Object
Dim value As Object
instance.Add(key, value)
public void Add (
Object key,
Object value
)
public:
virtual void Add (
Object^ key,
Object^ value
) sealed
public final void Add (
Object key,
Object value
)
public final function Add (
key : Object,
value : Object
)
Parameter
- key
Der Schlüssel des hinzuzufügenden Eintrags.
- value
Der Wert des hinzuzufügenden Eintrags. Der Wert kann NULL (Nothing in Visual Basic) sein.
Ausnahmen
| Ausnahmetyp | Bedingung |
|---|---|
key ist NULL (Nothing in Visual Basic). |
|
Ein Eintrag mit demselben Schlüssel ist bereits im ListDictionary vorhanden. |
Hinweise
Wenn bei einem Objekt keine Korrelation zwischen seinem Zustand und seinem Hashcodewert besteht, sollte es nicht als Schlüssel verwendet werden. Beispielsweise sollten String-Objekte bei der Verwendung als Schlüssel StringBuilder-Objekten vorgezogen werden.
Sie können die Item-Eigenschaft auch zum Hinzufügen neuer Elemente verwenden, indem Sie den Wert eines Schlüssels festlegen, der in ListDictionary nicht vorhanden ist, beispielsweise myCollection["myNonexistentKey"] = myValue. Wenn der angegebene Schlüssel jedoch bereits in ListDictionary vorhanden ist, wird der alte Wert beim Festlegen der Item-Eigenschaft überschrieben. Im Gegensatz dazu ändert die Add-Methode keine vorhandenen Elemente.
Diese Methode ist ein O(n)-Vorgang, wobei n der Count ist.
Beispiel
Im folgenden Codebeispiel werden Elemente einem ListDictionary hinzugefügt und aus diesem entfernt.
Imports System
Imports System.Collections
Imports System.Collections.Specialized
Public Class SamplesListDictionary
Public Shared Sub Main()
' Creates and initializes a new ListDictionary.
Dim myCol As New ListDictionary()
myCol.Add("Braeburn Apples", "1.49")
myCol.Add("Fuji Apples", "1.29")
myCol.Add("Gala Apples", "1.49")
myCol.Add("Golden Delicious Apples", "1.29")
myCol.Add("Granny Smith Apples", "0.89")
myCol.Add("Red Delicious Apples", "0.99")
' Displays the values in the ListDictionary in three different ways.
Console.WriteLine("Initial contents of the ListDictionary:")
PrintKeysAndValues(myCol)
' Deletes a key.
myCol.Remove("Plums")
Console.WriteLine("The collection contains the following elements after removing ""Plums"":")
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 IDictionary)
Console.WriteLine(" KEY VALUE")
Dim de As DictionaryEntry
For Each de In myCol
Console.WriteLine(" {0,-25} {1}", de.Key, de.Value)
Next de
Console.WriteLine()
End Sub 'PrintKeysAndValues
End Class 'SamplesListDictionary
'This code produces the following output.
'
'Initial contents of the ListDictionary:
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Gala Apples 1.49
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'The collection contains the following elements after removing "Plums":
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' Gala Apples 1.49
' Golden Delicious Apples 1.29
' Granny Smith Apples 0.89
' Red Delicious Apples 0.99
'
'The collection contains the following elements after it is cleared:
' KEY VALUE
'
using System;
using System.Collections;
using System.Collections.Specialized;
public class SamplesListDictionary {
public static void Main() {
// Creates and initializes a new ListDictionary.
ListDictionary myCol = new ListDictionary();
myCol.Add( "Braeburn Apples", "1.49" );
myCol.Add( "Fuji Apples", "1.29" );
myCol.Add( "Gala Apples", "1.49" );
myCol.Add( "Golden Delicious Apples", "1.29" );
myCol.Add( "Granny Smith Apples", "0.89" );
myCol.Add( "Red Delicious Apples", "0.99" );
// Displays the values in the ListDictionary in three different ways.
Console.WriteLine( "Initial contents of the ListDictionary:" );
PrintKeysAndValues( myCol );
// Deletes a key.
myCol.Remove( "Plums" );
Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" );
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( IDictionary myCol ) {
Console.WriteLine( " KEY VALUE" );
foreach ( DictionaryEntry de in myCol )
Console.WriteLine( " {0,-25} {1}", de.Key, de.Value );
Console.WriteLine();
}
}
/*
This code produces the following output.
Initial contents of the ListDictionary:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after removing "Plums":
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
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( IDictionary^ myCol )
{
Console::WriteLine( " KEY VALUE" );
IEnumerator^ myEnum = myCol->GetEnumerator();
while ( myEnum->MoveNext() )
{
DictionaryEntry de = safe_cast<DictionaryEntry>(myEnum->Current);
Console::WriteLine( " {0,-25} {1}", de.Key, de.Value );
}
Console::WriteLine();
}
int main()
{
// Creates and initializes a new ListDictionary.
ListDictionary^ myCol = gcnew ListDictionary;
myCol->Add( "Braeburn Apples", "1.49" );
myCol->Add( "Fuji Apples", "1.29" );
myCol->Add( "Gala Apples", "1.49" );
myCol->Add( "Golden Delicious Apples", "1.29" );
myCol->Add( "Granny Smith Apples", "0.89" );
myCol->Add( "Red Delicious Apples", "0.99" );
// Displays the values in the ListDictionary in three different ways.
Console::WriteLine( "Initial contents of the ListDictionary:" );
PrintKeysAndValues( myCol );
// Deletes a key.
myCol->Remove( "Plums" );
Console::WriteLine( "The collection contains the following elements after removing \"Plums\":" );
PrintKeysAndValues( myCol );
// Clears the entire collection.
myCol->Clear();
Console::WriteLine( "The collection contains the following elements after it is cleared:" );
PrintKeysAndValues( myCol );
}
/*
This code produces the following output.
Initial contents of the ListDictionary:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after removing "Plums":
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after it is cleared:
KEY VALUE
*/
import System.*;
import System.Collections.*;
import System.Collections.Specialized.*;
public class SamplesListDictionary
{
public static void main(String[] args)
{
// Creates and initializes a new ListDictionary.
ListDictionary myCol = new ListDictionary();
myCol.Add("Braeburn Apples", "1.49");
myCol.Add("Fuji Apples", "1.29");
myCol.Add("Gala Apples", "1.49");
myCol.Add("Golden Delicious Apples", "1.29");
myCol.Add("Granny Smith Apples", "0.89");
myCol.Add("Red Delicious Apples", "0.99");
// Displays the values in the ListDictionary in three different ways.
Console.WriteLine("Initial contents of the ListDictionary:");
PrintKeysAndValues(myCol);
// Deletes a key.
myCol.Remove("Plums");
Console.WriteLine("The collection contains the following elements"
+ " after removing \"Plums\":");
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(IDictionary myCol)
{
IEnumerator objEnum = myCol.GetEnumerator();
Console.WriteLine(" KEY VALUE");
while (objEnum.MoveNext()) {
DictionaryEntry de = (DictionaryEntry)objEnum.get_Current();
Console.WriteLine(" {0,-25} {1}", de.get_Key(), de.get_Value());
}
Console.WriteLine();
}//PrintKeysAndValues
} //SamplesListDictionary
/*
This code produces the following output.
Initial contents of the ListDictionary:
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after removing "Plums":
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
Gala Apples 1.49
Golden Delicious Apples 1.29
Granny Smith Apples 0.89
Red Delicious Apples 0.99
The collection contains the following elements after it is cleared:
KEY VALUE
*/
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
ListDictionary-Klasse
ListDictionary-Member
System.Collections.Specialized-Namespace
Remove
Item
IDictionary.Add