ListDictionary.Clear Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Odebere všechny položky z objektu ListDictionary.
public:
virtual void Clear();
public void Clear ();
abstract member Clear : unit -> unit
override this.Clear : unit -> unit
Public Sub Clear ()
Implementuje
Příklady
Následující příklad kódu přidá a odebere elementy z objektu 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
'
Poznámky
Count je nastavená na nulu a uvolní se také odkazy na jiné objekty z prvků kolekce.
Tato metoda je operace O(1).
Platí pro
Viz také
Spolupracujte s námi na GitHubu
Zdroj tohoto obsahu najdete na GitHubu, kde můžete také vytvářet a kontrolovat problémy a žádosti o přijetí změn. Další informace najdete v našem průvodci pro přispěvatele.