Export (0) Print
Expand All

HybridDictionary.Clear Method

Removes all entries from the HybridDictionary.

Namespace:  System.Collections.Specialized
Assembly:  System (in System.dll)

'Declaration
Public Sub Clear

Implements

IDictionary.Clear

Count is set to zero, and references to other objects from elements of the collection are also released.

If the collection is already stored in a Hashtable, the collection remains in the Hashtable.

This method is an O(n) operation, where n is Count.

The following code example adds to and removes elements from a HybridDictionary.

Imports System
Imports System.Collections
Imports System.Collections.Specialized

Public Class SamplesHybridDictionary   

   Public Shared Sub Main()

      ' Creates and initializes a new HybridDictionary. 
      Dim myCol As New HybridDictionary()
      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")
      myCol.Add("Plantain Bananas", "1.49")
      myCol.Add("Yellow Bananas", "0.79")
      myCol.Add("Strawberries", "3.33")
      myCol.Add("Cranberries", "5.98")
      myCol.Add("Navel Oranges", "1.29")
      myCol.Add("Grapes", "1.99")
      myCol.Add("Honeydew Melon", "0.59")
      myCol.Add("Seedless Watermelon", "0.49")
      myCol.Add("Pineapple", "1.49")
      myCol.Add("Nectarine", "1.99")
      myCol.Add("Plums", "1.69")
      myCol.Add("Peaches", "1.99")

      ' Displays the values in the HybridDictionary in three different ways.
      Console.WriteLine("Initial contents of the HybridDictionary:")
      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 'SamplesHybridDictionary 


'This code produces the following output. 

'Initial contents of the HybridDictionary: 
'   KEY                       VALUE 
'   Seedless Watermelon       0.49 
'   Nectarine                 1.99 
'   Cranberries               5.98 
'   Plantain Bananas          1.49 
'   Honeydew Melon            0.59 
'   Pineapple                 1.49 
'   Strawberries              3.33 
'   Grapes                    1.99 
'   Braeburn Apples           1.49 
'   Peaches                   1.99 
'   Red Delicious Apples      0.99 
'   Golden Delicious Apples   1.29 
'   Yellow Bananas            0.79 
'   Granny Smith Apples       0.89 
'   Gala Apples               1.49 
'   Plums                     1.69 
'   Navel Oranges             1.29 
'   Fuji Apples               1.29 

'The collection contains the following elements after removing "Plums": 
'   KEY                       VALUE 
'   Seedless Watermelon       0.49 
'   Nectarine                 1.99 
'   Cranberries               5.98 
'   Plantain Bananas          1.49 
'   Honeydew Melon            0.59 
'   Pineapple                 1.49 
'   Strawberries              3.33 
'   Grapes                    1.99 
'   Braeburn Apples           1.49 
'   Peaches                   1.99 
'   Red Delicious Apples      0.99 
'   Golden Delicious Apples   1.29 
'   Yellow Bananas            0.79 
'   Granny Smith Apples       0.89 
'   Gala Apples               1.49 
'   Navel Oranges             1.29 
'   Fuji Apples               1.29 

'The collection contains the following elements after it is cleared: 
'   KEY                       VALUE 
'

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft