HybridDictionary Klasse

Definition

Implementiert IDictionary. Dabei wird bei kleinen Auflistungen ein ListDictionary verwendet, und bei größeren Auflistungen wird zu einer Hashtable gewechselt.

public ref class HybridDictionary : System::Collections::IDictionary
public class HybridDictionary : System.Collections.IDictionary
[System.Serializable]
public class HybridDictionary : System.Collections.IDictionary
type HybridDictionary = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
[<System.Serializable>]
type HybridDictionary = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
Public Class HybridDictionary
Implements IDictionary
Vererbung
HybridDictionary
Abgeleitet
Attribute
Implementiert

Beispiele

Im folgenden Codebeispiel werden mehrere der Eigenschaften und Methoden von HybridDictionary.

#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Collections::Specialized;

void PrintKeysAndValues1( IDictionary^ myCol );
void PrintKeysAndValues2( IDictionary^ myCol );
void PrintKeysAndValues3( HybridDictionary^ myCol );
int main()
{
   
   // Creates and initializes a new HybridDictionary.
   HybridDictionary^ myCol = gcnew 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" );

   // Display the contents of the collection using for each. This is the preferred method.
   Console::WriteLine( "Displays the elements using for each:" );
   PrintKeysAndValues1( myCol );

   // Display the contents of the collection using the enumerator.
   Console::WriteLine( "Displays the elements using the IDictionaryEnumerator:" );
   PrintKeysAndValues2( myCol );

   // Display the contents of the collection using the Keys, Values, Count, and Item properties.
   Console::WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" );
   PrintKeysAndValues3( myCol );

   // Copies the HybridDictionary to an array with DictionaryEntry elements.
   array<DictionaryEntry>^myArr = gcnew array<DictionaryEntry>(myCol->Count);
   myCol->CopyTo( myArr, 0 );

   // Displays the values in the array.
   Console::WriteLine( "Displays the elements in the array:" );
   Console::WriteLine( "   KEY                       VALUE" );
   for ( int i = 0; i < myArr->Length; i++ )
      Console::WriteLine( "   {0,-25} {1}", myArr[ i ].Key, myArr[ i ].Value );
   Console::WriteLine();

   // Searches for a key.
   if ( myCol->Contains( "Kiwis" ) )
      Console::WriteLine( "The collection contains the key \"Kiwis\"." );
   else
      Console::WriteLine( "The collection does not contain the key \"Kiwis\"." );

   Console::WriteLine();

   // Deletes a key.
   myCol->Remove( "Plums" );
   Console::WriteLine( "The collection contains the following elements after removing \"Plums\":" );
   PrintKeysAndValues1( myCol );

   // Clears the entire collection.
   myCol->Clear();
   Console::WriteLine( "The collection contains the following elements after it is cleared:" );
   PrintKeysAndValues1( myCol );
}

// Uses the for each statement which hides the complexity of the enumerator.
// NOTE: The for each statement is the preferred way of enumerating the contents of a collection.
void PrintKeysAndValues1( IDictionary^ myCol )  {
   Console::WriteLine( "   KEY                       VALUE" );
   for each ( DictionaryEntry^ de in myCol )
      Console::WriteLine( "   {0,-25} {1}", de->Key, de->Value );
   Console::WriteLine();
}

// Uses the enumerator. 
void PrintKeysAndValues2( IDictionary^ myCol )
{
   IDictionaryEnumerator^ myEnumerator = myCol->GetEnumerator();
   Console::WriteLine( "   KEY                       VALUE" );
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( "   {0,-25} {1}", myEnumerator->Key, myEnumerator->Value );

   Console::WriteLine();
}

// Uses the Keys, Values, Count, and Item properties.
void PrintKeysAndValues3( HybridDictionary^ myCol )
{
   array<String^>^myKeys = gcnew array<String^>(myCol->Count);
   myCol->Keys->CopyTo( myKeys, 0 );
   Console::WriteLine( "   INDEX KEY                       VALUE" );
   for ( int i = 0; i < myCol->Count; i++ )
      Console::WriteLine( "   {0,-5} {1,-25} {2}", i, myKeys[ i ], myCol[ myKeys[ i ] ] );
   Console::WriteLine();
}

/*
This code produces output similar to the following:

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

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

Displays the elements using the Keys, Values, Count, and Item properties:
   INDEX KEY                       VALUE
   0     Strawberries              3.33
   1     Yellow Bananas            0.79
   2     Cranberries               5.98
   3     Grapes                    1.99
   4     Granny Smith Apples       0.89
   5     Seedless Watermelon       0.49
   6     Honeydew Melon            0.59
   7     Red Delicious Apples      0.99
   8     Navel Oranges             1.29
   9     Fuji Apples               1.29
   10    Plantain Bananas          1.49
   11    Gala Apples               1.49
   12    Pineapple                 1.49
   13    Plums                     1.69
   14    Braeburn Apples           1.49
   15    Peaches                   1.99
   16    Golden Delicious Apples   1.29
   17    Nectarine                 1.99

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

The collection does not contain the key "Kiwis".

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

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

*/
using System;
using System.Collections;
using System.Collections.Specialized;

public class SamplesHybridDictionary  {

   public static void Main()  {

      // Creates and initializes a new HybridDictionary.
      HybridDictionary myCol = 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" );

      // Display the contents of the collection using foreach. This is the preferred method.
      Console.WriteLine( "Displays the elements using foreach:" );
      PrintKeysAndValues1( myCol );

      // Display the contents of the collection using the enumerator.
      Console.WriteLine( "Displays the elements using the IDictionaryEnumerator:" );
      PrintKeysAndValues2( myCol );

      // Display the contents of the collection using the Keys, Values, Count, and Item properties.
      Console.WriteLine( "Displays the elements using the Keys, Values, Count, and Item properties:" );
      PrintKeysAndValues3( myCol );

      // Copies the HybridDictionary to an array with DictionaryEntry elements.
      DictionaryEntry[] myArr = new DictionaryEntry[myCol.Count];
      myCol.CopyTo( myArr, 0 );

      // Displays the values in the array.
      Console.WriteLine( "Displays the elements in the array:" );
      Console.WriteLine( "   KEY                       VALUE" );
      for ( int i = 0; i < myArr.Length; i++ )
         Console.WriteLine( "   {0,-25} {1}", myArr[i].Key, myArr[i].Value );
      Console.WriteLine();

      // Searches for a key.
      if ( myCol.Contains( "Kiwis" ) )
         Console.WriteLine( "The collection contains the key \"Kiwis\"." );
      else
         Console.WriteLine( "The collection does not contain the key \"Kiwis\"." );
      Console.WriteLine();

      // Deletes a key.
      myCol.Remove( "Plums" );
      Console.WriteLine( "The collection contains the following elements after removing \"Plums\":" );
      PrintKeysAndValues1( myCol );

      // Clears the entire collection.
      myCol.Clear();
      Console.WriteLine( "The collection contains the following elements after it is cleared:" );
      PrintKeysAndValues1( myCol );
   }

   // Uses the foreach statement which hides the complexity of the enumerator.
   // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
   public static void PrintKeysAndValues1( IDictionary myCol )  {
      Console.WriteLine( "   KEY                       VALUE" );
      foreach ( DictionaryEntry de in myCol )
         Console.WriteLine( "   {0,-25} {1}", de.Key, de.Value );
      Console.WriteLine();
   }

   // Uses the enumerator.
   // NOTE: The foreach statement is the preferred way of enumerating the contents of a collection.
   public static void PrintKeysAndValues2( IDictionary myCol )  {
      IDictionaryEnumerator myEnumerator = myCol.GetEnumerator();
      Console.WriteLine( "   KEY                       VALUE" );
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "   {0,-25} {1}", myEnumerator.Key, myEnumerator.Value );
      Console.WriteLine();
   }

   // Uses the Keys, Values, Count, and Item properties.
   public static void PrintKeysAndValues3( HybridDictionary myCol )  {
      String[] myKeys = new String[myCol.Count];
      myCol.Keys.CopyTo( myKeys, 0 );

      Console.WriteLine( "   INDEX KEY                       VALUE" );
      for ( int i = 0; i < myCol.Count; i++ )
         Console.WriteLine( "   {0,-5} {1,-25} {2}", i, myKeys[i], myCol[myKeys[i]] );
      Console.WriteLine();
   }
}

/*
This code produces output similar to the following:

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

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

Displays the elements using the Keys, Values, Count, and Item properties:
   INDEX KEY                       VALUE
   0     Strawberries              3.33
   1     Yellow Bananas            0.79
   2     Cranberries               5.98
   3     Grapes                    1.99
   4     Granny Smith Apples       0.89
   5     Seedless Watermelon       0.49
   6     Honeydew Melon            0.59
   7     Red Delicious Apples      0.99
   8     Navel Oranges             1.29
   9     Fuji Apples               1.29
   10    Plantain Bananas          1.49
   11    Gala Apples               1.49
   12    Pineapple                 1.49
   13    Plums                     1.69
   14    Braeburn Apples           1.49
   15    Peaches                   1.99
   16    Golden Delicious Apples   1.29
   17    Nectarine                 1.99

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

The collection does not contain the key "Kiwis".

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

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

*/
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")

      ' Display the contents of the collection using For Each. This is the preferred method.
      Console.WriteLine("Displays the elements using For Each:")
      PrintKeysAndValues1(myCol)

      ' Display the contents of the collection using the enumerator.
      Console.WriteLine("Displays the elements using the IDictionaryEnumerator:")
      PrintKeysAndValues2(myCol)

      ' Display the contents of the collection using the Keys, Values, Count, and Item properties.
      Console.WriteLine("Displays the elements using the Keys, Values, Count, and Item properties:")
      PrintKeysAndValues3(myCol)

      ' Copies the HybridDictionary to an array with DictionaryEntry elements.
      Dim myArr(myCol.Count) As DictionaryEntry
      myCol.CopyTo(myArr, 0)

      ' Displays the values in the array.
      Console.WriteLine("Displays the elements in the array:")
      Console.WriteLine("   KEY                       VALUE")
      Dim i As Integer
      For i = 0 To myArr.Length - 1
         Console.WriteLine("   {0,-25} {1}", myArr(i).Key, myArr(i).Value)
      Next i
      Console.WriteLine()

      ' Searches for a key.
      If myCol.Contains("Kiwis") Then
         Console.WriteLine("The collection contains the key ""Kiwis"".")
      Else
         Console.WriteLine("The collection does not contain the key ""Kiwis"".")
      End If
      Console.WriteLine()

      ' Deletes a key.
      myCol.Remove("Plums")
      Console.WriteLine("The collection contains the following elements after removing ""Plums"":")
      PrintKeysAndValues1(myCol)

      ' Clears the entire collection.
      myCol.Clear()
      Console.WriteLine("The collection contains the following elements after it is cleared:")
      PrintKeysAndValues1(myCol)

   End Sub

   ' Uses the For Each statement which hides the complexity of the enumerator.
   ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
   Public Shared Sub PrintKeysAndValues1(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


   ' Uses the enumerator. 
   ' NOTE: The For Each statement is the preferred way of enumerating the contents of a collection.
   Public Shared Sub PrintKeysAndValues2(myCol As IDictionary)
      Dim myEnumerator As IDictionaryEnumerator = myCol.GetEnumerator()

      Console.WriteLine("   KEY                       VALUE")
      While myEnumerator.MoveNext()
         Console.WriteLine("   {0,-25} {1}", myEnumerator.Key, myEnumerator.Value)
      End While
      Console.WriteLine()

   End Sub


   ' Uses the Keys, Values, Count, and Item properties.
   Public Shared Sub PrintKeysAndValues3(myCol As HybridDictionary)
      Dim myKeys(myCol.Count) As [String]
      myCol.Keys.CopyTo(myKeys, 0)

      Console.WriteLine("   INDEX KEY                       VALUE")
      Dim i As Integer
      For i = 0 To myCol.Count - 1
         Console.WriteLine("   {0,-5} {1,-25} {2}", i, myKeys(i), myCol(myKeys(i)))
      Next i
      Console.WriteLine()

   End Sub

End Class


'This code produces output similar to the following:
'
'Displays the elements using For Each:
'   KEY                       VALUE
'   Strawberries              3.33
'   Yellow Bananas            0.79
'   Cranberries               5.98
'   Grapes                    1.99
'   Granny Smith Apples       0.89
'   Seedless Watermelon       0.49
'   Honeydew Melon            0.59
'   Red Delicious Apples      0.99
'   Navel Oranges             1.29
'   Fuji Apples               1.29
'   Plantain Bananas          1.49
'   Gala Apples               1.49
'   Pineapple                 1.49
'   Plums                     1.69
'   Braeburn Apples           1.49
'   Peaches                   1.99
'   Golden Delicious Apples   1.29
'   Nectarine                 1.99
'
'Displays the elements using the IDictionaryEnumerator:
'   KEY                       VALUE
'   Strawberries              3.33
'   Yellow Bananas            0.79
'   Cranberries               5.98
'   Grapes                    1.99
'   Granny Smith Apples       0.89
'   Seedless Watermelon       0.49
'   Honeydew Melon            0.59
'   Red Delicious Apples      0.99
'   Navel Oranges             1.29
'   Fuji Apples               1.29
'   Plantain Bananas          1.49
'   Gala Apples               1.49
'   Pineapple                 1.49
'   Plums                     1.69
'   Braeburn Apples           1.49
'   Peaches                   1.99
'   Golden Delicious Apples   1.29
'   Nectarine                 1.99
'
'Displays the elements using the Keys, Values, Count, and Item properties:
'   INDEX KEY                       VALUE
'   0     Strawberries              3.33
'   1     Yellow Bananas            0.79
'   2     Cranberries               5.98
'   3     Grapes                    1.99
'   4     Granny Smith Apples       0.89
'   5     Seedless Watermelon       0.49
'   6     Honeydew Melon            0.59
'   7     Red Delicious Apples      0.99
'   8     Navel Oranges             1.29
'   9     Fuji Apples               1.29
'   10    Plantain Bananas          1.49
'   11    Gala Apples               1.49
'   12    Pineapple                 1.49
'   13    Plums                     1.69
'   14    Braeburn Apples           1.49
'   15    Peaches                   1.99
'   16    Golden Delicious Apples   1.29
'   17    Nectarine                 1.99
'
'Displays the elements in the array:
'   KEY                       VALUE
'   Strawberries              3.33
'   Yellow Bananas            0.79
'   Cranberries               5.98
'   Grapes                    1.99
'   Granny Smith Apples       0.89
'   Seedless Watermelon       0.49
'   Honeydew Melon            0.59
'   Red Delicious Apples      0.99
'   Navel Oranges             1.29
'   Fuji Apples               1.29
'   Plantain Bananas          1.49
'   Gala Apples               1.49
'   Pineapple                 1.49
'   Plums                     1.69
'   Braeburn Apples           1.49
'   Peaches                   1.99
'   Golden Delicious Apples   1.29
'   Nectarine                 1.99
'
'The collection does not contain the key "Kiwis".
'
'The collection contains the following elements after removing "Plums":
'   KEY                       VALUE
'   Strawberries              3.33
'   Yellow Bananas            0.79
'   Cranberries               5.98
'   Grapes                    1.99
'   Granny Smith Apples       0.89
'   Seedless Watermelon       0.49
'   Honeydew Melon            0.59
'   Red Delicious Apples      0.99
'   Navel Oranges             1.29
'   Fuji Apples               1.29
'   Plantain Bananas          1.49
'   Gala Apples               1.49
'   Pineapple                 1.49
'   Braeburn Apples           1.49
'   Peaches                   1.99
'   Golden Delicious Apples   1.29
'   Nectarine                 1.99
'
'The collection contains the following elements after it is cleared:
'   KEY                       VALUE

Hinweise

Diese Klasse wird für Fälle empfohlen, in denen die Anzahl der Elemente in einem Wörterbuch unbekannt ist. Es nutzt die verbesserte Leistung einer ListDictionary mit kleinen Sammlungen und bietet die Flexibilität, zu einer Hashtable zu wechseln, die größere Sammlungen besser behandelt als ListDictionary.

Wenn die anfängliche Größe der Auflistung größer als die optimale Größe für eine ListDictionaryAuflistung ist, wird die Auflistung in einer Hashtable gespeichert, um den Aufwand des Kopierens von Elementen von der ListDictionary in eine .Hashtable

Der Konstruktor akzeptiert einen booleschen Parameter, mit dem der Benutzer angeben kann, ob die Auflistung den Fall ignoriert, wenn Zeichenfolgen verglichen werden. Wenn die Auflistung Groß-/Kleinschreibung beachtet, verwendet sie die Implementierungen des Schlüssels Object.GetHashCode und Object.Equals. Wenn die Auflistung groß-insensitiv ist, führt sie einen einfachen ordinalen Vergleich zwischen Groß- und Kleinschreibung aus, der nur den Groß-/Kleinschreibungsregeln der invarianten Kultur entspricht. Standardmäßig ist die Auflistung groß- und kleinschreibungsempfindlich. Weitere Informationen zur invarianten Kultur finden Sie unter System.Globalization.CultureInfo.

Ein Schlüssel kann nicht null sein, aber ein Wert kann.

Die foreach Anweisung der C#-Sprache (For Each in Visual Basic) gibt ein Objekt des Typs der Elemente in der Auflistung zurück. Da jedes Element ein HybridDictionary Schlüssel-/Wertpaar ist, ist der Elementtyp nicht der Typ des Schlüssels oder des Typs des Werts. Stattdessen lautet DictionaryEntryder Elementtyp . Beispiel:

for each (DictionaryEntry^ de in myHybridDictionary)
{
    //...
}
foreach (DictionaryEntry de in myHybridDictionary)
{
    //...
}
For Each de In myHybridDictionary
    '...
Next

Die foreach Anweisung ist ein Wrapper um den Aufzählungsator, der nur das Lesen von, nicht schreiben in die Auflistung ermöglicht.

Konstruktoren

HybridDictionary()

Erstellt ein leeres HybridDictionary, bei dem die Groß- und Kleinschreibung berücksichtigt wird.

HybridDictionary(Boolean)

Erstellt ein leeres HybridDictionary mit der angegebenen Berücksichtigung der Groß- und Kleinschreibung.

HybridDictionary(Int32)

Erstellt ein HybridDictionary, bei dem die Groß- und Kleinschreibung berücksichtigt wird, mit der angegebenen Anfangsgröße.

HybridDictionary(Int32, Boolean)

Erstellt ein HybridDictionary, bei dem die Groß- und Kleinschreibung berücksichtigt wird, mit der angegebenen Anfangsgröße.

Eigenschaften

Count

Ruft die Anzahl der Schlüssel-Wert-Paare im HybridDictionary ab.

IsFixedSize

Ruft einen Wert ab, der angibt, ob das HybridDictionary eine feste Größe aufweist.

IsReadOnly

Ruft einen Wert ab, der angibt, ob das HybridDictionary schreibgeschützt ist.

IsSynchronized

Ruft einen Wert ab, der angibt, ob HybridDictionary synchronisiert (threadsicher) ist.

Item[Object]

Ruft den Wert ab, der dem angegebenen Schlüssel zugeordnet ist, oder legt diesen fest.

Keys

Ruft eine ICollection ab, die die Schlüssel in der HybridDictionary enthält.

SyncRoot

Ruft ein Objekt ab, mit dem der Zugriff auf HybridDictionary synchronisiert werden kann.

Values

Ruft eine ICollection ab, die die Werte im HybridDictionary enthält.

Methoden

Add(Object, Object)

Fügt dem HybridDictionary einen Eintrag mit dem angegebenen Schlüssel und Wert hinzu.

Clear()

Entfernt alle Einträge aus dem HybridDictionary.

Contains(Object)

Stellt fest, ob der HybridDictionary einen bestimmten Schlüssel enthält.

CopyTo(Array, Int32)

Kopiert die HybridDictionary-Einträge an den angegebenen Index in einer eindimensionalen Array-Instanz.

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

(Geerbt von Object)
GetEnumerator()

Gibt einen IDictionaryEnumerator zurück, der HybridDictionary durchläuft.

GetHashCode()

Fungiert als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft den Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
Remove(Object)

Entfernt der Eintrag mit dem angegebenen Schlüssel aus dem HybridDictionary.

ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Explizite Schnittstellenimplementierungen

IEnumerable.GetEnumerator()

Gibt einen IEnumerator zurück, der HybridDictionary durchläuft.

Erweiterungsmethoden

Cast<TResult>(IEnumerable)

Wandelt die Elemente eines IEnumerable in den angegebenen Typ um

OfType<TResult>(IEnumerable)

Filtert die Elemente eines IEnumerable anhand eines angegebenen Typs

AsParallel(IEnumerable)

Ermöglicht die Parallelisierung einer Abfrage.

AsQueryable(IEnumerable)

Konvertiert einen IEnumerable in einen IQueryable.

Gilt für

Threadsicherheit

Öffentliche statische Elemente (Sharedin Visual Basic) dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Diese Implementierung stellt keinen synchronisierten (threadsicheren) Wrapper für eine HybridDictionary, aber abgeleitete Klassen können ihre eigenen synchronisierten Versionen der HybridDictionary SyncRoot Eigenschaft 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 die Ausnahmen, die aus von anderen Threads stammenden Änderungen resultieren, abfangen.

Siehe auch