ListDictionary.Remove(Object) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Supprime de ListDictionary l'entrée avec la clé spécifiée.
public:
virtual void Remove(System::Object ^ key);
public void Remove (object key);
abstract member Remove : obj -> unit
override this.Remove : obj -> unit
Public Sub Remove (key As Object)
Paramètres
- key
- Object
Clé de l'entrée à supprimer.
Implémente
Exceptions
key
a la valeur null
.
Exemples
L’exemple de code suivant ajoute à et supprime des éléments d’un ListDictionary.
#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( "Gala Apples" );
Console::WriteLine( "The collection contains the following elements after removing \"Gala Apples\":" );
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 "Gala Apples":
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
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( "Gala Apples" );
Console.WriteLine( "The collection contains the following elements after removing \"Gala Apples\":" );
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 "Gala Apples":
KEY VALUE
Braeburn Apples 1.49
Fuji Apples 1.29
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
*/
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("Gala Apples")
Console.WriteLine("The collection contains the following elements after removing ""Gala Apples"":")
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 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
End Class
'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 "Gala Apples":
' KEY VALUE
' Braeburn Apples 1.49
' Fuji Apples 1.29
' 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
'
Remarques
Si le ListDictionary ne contient pas d’élément avec la clé spécifiée, le ListDictionary reste inchangé. Aucune exception n’est générée.
Cette méthode est une opération O(n
), où n
est Count.