DictionaryBase Klasse

Definition

Stellt die abstract-Basisklasse für eine stark typisierte Auflistung von Schlüssel-Wert-Paaren bereit.

public ref class DictionaryBase abstract : System::Collections::IDictionary
public abstract class DictionaryBase : System.Collections.IDictionary
[System.Serializable]
public abstract class DictionaryBase : System.Collections.IDictionary
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class DictionaryBase : System.Collections.IDictionary
type DictionaryBase = class
    interface ICollection
    interface IEnumerable
    interface IDictionary
[<System.Serializable>]
type DictionaryBase = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type DictionaryBase = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
Public MustInherit Class DictionaryBase
Implements IDictionary
Vererbung
DictionaryBase
Abgeleitet
Attribute
Implementiert

Beispiele

Das folgende Codebeispiel implementiert die DictionaryBase -Klasse und verwendet diese Implementierung, um ein Wörterbuch mit String Schlüsseln und Werten zu erstellen, die maximal Length 5 Zeichen lang sind.

using namespace System;
using namespace System::Collections;

public ref class ShortStringDictionary: public DictionaryBase
{
public:

   property String^ Item [String^]
   {
      String^ get( String^ key )
      {
         return (dynamic_cast<String^>(Dictionary[ key ]));
      }

      void set( String^ value, String^ key )
      {
         Dictionary[ key ] = value;
      }
   }

   property ICollection^ Keys 
   {
      ICollection^ get()
      {
         return (Dictionary->Keys);
      }
   }

   property ICollection^ Values 
   {
      ICollection^ get()
      {
         return (Dictionary->Values);
      }
   }
   void Add( String^ key, String^ value )
   {
      Dictionary->Add( key, value );
   }

   bool Contains( String^ key )
   {
      return (Dictionary->Contains( key ));
   }

   void Remove( String^ key )
   {
      Dictionary->Remove( key );
   }


protected:
   virtual void OnInsert( Object^ key, Object^ value ) override
   {
      if ( key->GetType() != Type::GetType( "System.String" ) )
            throw gcnew ArgumentException( "key must be of type String.","key" );
      else
      {
         String^ strKey = dynamic_cast<String^>(key);
         if ( strKey->Length > 5 )
                  throw gcnew ArgumentException( "key must be no more than 5 characters in length.","key" );
      }

      if ( value->GetType() != Type::GetType( "System.String" ) )
            throw gcnew ArgumentException( "value must be of type String.","value" );
      else
      {
         String^ strValue = dynamic_cast<String^>(value);
         if ( strValue->Length > 5 )
                  throw gcnew ArgumentException( "value must be no more than 5 characters in length.","value" );
      }
   }

   virtual void OnRemove( Object^ key, Object^ /*value*/ ) override
   {
      if ( key->GetType() != Type::GetType( "System.String" ) )
            throw gcnew ArgumentException( "key must be of type String.","key" );
      else
      {
         String^ strKey = dynamic_cast<String^>(key);
         if ( strKey->Length > 5 )
                  throw gcnew ArgumentException( "key must be no more than 5 characters in length.","key" );
      }
   }

   virtual void OnSet( Object^ key, Object^ /*oldValue*/, Object^ newValue ) override
   {
      if ( key->GetType() != Type::GetType( "System.String" ) )
            throw gcnew ArgumentException( "key must be of type String.","key" );
      else
      {
         String^ strKey = dynamic_cast<String^>(key);
         if ( strKey->Length > 5 )
                  throw gcnew ArgumentException( "key must be no more than 5 characters in length.","key" );
      }

      if ( newValue->GetType() != Type::GetType( "System.String" ) )
            throw gcnew ArgumentException( "newValue must be of type String.","newValue" );
      else
      {
         String^ strValue = dynamic_cast<String^>(newValue);
         if ( strValue->Length > 5 )
                  throw gcnew ArgumentException( "newValue must be no more than 5 characters in length.","newValue" );
      }
   }

   virtual void OnValidate( Object^ key, Object^ value ) override
   {
      if ( key->GetType() != Type::GetType( "System.String" ) )
            throw gcnew ArgumentException( "key must be of type String.","key" );
      else
      {
         String^ strKey = dynamic_cast<String^>(key);
         if ( strKey->Length > 5 )
                  throw gcnew ArgumentException( "key must be no more than 5 characters in length.","key" );
      }

      if ( value->GetType() != Type::GetType( "System.String" ) )
            throw gcnew ArgumentException( "value must be of type String.","value" );
      else
      {
         String^ strValue = dynamic_cast<String^>(value);
         if ( strValue->Length > 5 )
                  throw gcnew ArgumentException( "value must be no more than 5 characters in length.","value" );
      }
   }

};

void PrintKeysAndValues2( ShortStringDictionary^ myCol );
void PrintKeysAndValues3( ShortStringDictionary^ myCol );
int main()
{
   // Creates and initializes a new DictionaryBase.
   ShortStringDictionary^ mySSC = gcnew ShortStringDictionary;

   // Adds elements to the collection.
   mySSC->Add( "One", "a" );
   mySSC->Add( "Two", "ab" );
   mySSC->Add( "Three", "abc" );
   mySSC->Add( "Four", "abcd" );
   mySSC->Add( "Five", "abcde" );

   // Display the contents of the collection using the enumerator.
   Console::WriteLine( "Contents of the collection (using enumerator):" );
   PrintKeysAndValues2( mySSC );

   // Display the contents of the collection using the Keys property and the Item property.
   Console::WriteLine( "Initial contents of the collection (using Keys and Item):" );
   PrintKeysAndValues3( mySSC );

   // Tries to add a value that is too long.
   try
   {
      mySSC->Add( "Ten", "abcdefghij" );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( e );
   }

   // Tries to add a key that is too long.
   try
   {
      mySSC->Add( "Eleven", "ijk" );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( e );
   }

   Console::WriteLine();

   // Searches the collection with Contains.
   Console::WriteLine( "Contains \"Three\": {0}", mySSC->Contains( "Three" ) );
   Console::WriteLine( "Contains \"Twelve\": {0}", mySSC->Contains( "Twelve" ) );
   Console::WriteLine();

   // Removes an element from the collection.
   mySSC->Remove( "Two" );

   // Displays the contents of the collection.
   Console::WriteLine( "After removing \"Two\":" );
   PrintKeysAndValues2( mySSC );
}

// Uses the enumerator. 
void PrintKeysAndValues2( ShortStringDictionary^ myCol )
{
   DictionaryEntry myDE;
   System::Collections::IEnumerator^ myEnumerator = myCol->GetEnumerator();
   while ( myEnumerator->MoveNext() )
      if ( myEnumerator->Current != nullptr )
   {
      myDE =  *dynamic_cast<DictionaryEntry^>(myEnumerator->Current);
      Console::WriteLine( "   {0,-5} : {1}", myDE.Key, myDE.Value );
   }

   Console::WriteLine();
}


// Uses the Keys property and the Item property.
void PrintKeysAndValues3( ShortStringDictionary^ myCol )
{
   ICollection^ myKeys = myCol->Keys;
   IEnumerator^ myEnum1 = myKeys->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      String^ k = safe_cast<String^>(myEnum1->Current);
      Console::WriteLine( "   {0,-5} : {1}", k, myCol->Item[ k ] );
   }

   Console::WriteLine();
}

/* 
This code produces the following output.

Contents of the collection (using enumerator):
   Three : abc
   Five  : abcde
   Two   : ab
   One   : a
   Four  : abcd

Initial contents of the collection (using Keys and Item):
   Three : abc
   Five  : abcde
   Two   : ab
   One   : a
   Four  : abcd

System.ArgumentException: value must be no more than 5 characters in length.
Parameter name: value
   at ShortStringDictionary.OnValidate(Object key, Object value)
   at System.Collections.DictionaryBase.System.Collections.IDictionary.Add(Object key, Object value)
   at SamplesDictionaryBase.Main()
System.ArgumentException: key must be no more than 5 characters in length.
Parameter name: key
   at ShortStringDictionary.OnValidate(Object key, Object value)
   at System.Collections.DictionaryBase.System.Collections.IDictionary.Add(Object key, Object value)
   at SamplesDictionaryBase.Main()

Contains "Three": True
Contains "Twelve": False

After removing "Two":
   Three : abc
   Five  : abcde
   One   : a
   Four  : abcd

*/
using System;
using System.Collections;

public class ShortStringDictionary : DictionaryBase  {

   public String this[ String key ]  {
      get  {
         return( (String) Dictionary[key] );
      }
      set  {
         Dictionary[key] = value;
      }
   }

   public ICollection Keys  {
      get  {
         return( Dictionary.Keys );
      }
   }

   public ICollection Values  {
      get  {
         return( Dictionary.Values );
      }
   }

   public void Add( String key, String value )  {
      Dictionary.Add( key, value );
   }

   public bool Contains( String key )  {
      return( Dictionary.Contains( key ) );
   }

   public void Remove( String key )  {
      Dictionary.Remove( key );
   }

   protected override void OnInsert( Object key, Object value )  {
      if ( key.GetType() != typeof(System.String) )
        {
            throw new ArgumentException( "key must be of type String.", "key" );
        }
        else  {
         String strKey = (String) key;
         if ( strKey.Length > 5 )
            throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
      }

      if ( value.GetType() != typeof(System.String) )
        {
            throw new ArgumentException( "value must be of type String.", "value" );
        }
        else  {
         String strValue = (String) value;
         if ( strValue.Length > 5 )
            throw new ArgumentException( "value must be no more than 5 characters in length.", "value" );
      }
   }

   protected override void OnRemove( Object key, Object value )  {
      if ( key.GetType() != typeof(System.String) )
        {
            throw new ArgumentException( "key must be of type String.", "key" );
        }
        else  {
         String strKey = (String) key;
         if ( strKey.Length > 5 )
            throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
      }
   }

   protected override void OnSet( Object key, Object oldValue, Object newValue )  {
      if ( key.GetType() != typeof(System.String) )
        {
            throw new ArgumentException( "key must be of type String.", "key" );
        }
        else  {
         String strKey = (String) key;
         if ( strKey.Length > 5 )
            throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
      }

      if ( newValue.GetType() != typeof(System.String) )
        {
            throw new ArgumentException( "newValue must be of type String.", "newValue" );
        }
        else  {
         String strValue = (String) newValue;
         if ( strValue.Length > 5 )
            throw new ArgumentException( "newValue must be no more than 5 characters in length.", "newValue" );
      }
   }

   protected override void OnValidate( Object key, Object value )  {
      if ( key.GetType() != typeof(System.String) )
        {
            throw new ArgumentException( "key must be of type String.", "key" );
        }
        else  {
         String strKey = (String) key;
         if ( strKey.Length > 5 )
            throw new ArgumentException( "key must be no more than 5 characters in length.", "key" );
      }

      if ( value.GetType() != typeof(System.String) )
        {
            throw new ArgumentException( "value must be of type String.", "value" );
        }
        else  {
         String strValue = (String) value;
         if ( strValue.Length > 5 )
            throw new ArgumentException( "value must be no more than 5 characters in length.", "value" );
      }
   }
}

public class SamplesDictionaryBase  {

   public static void Main()  {

      // Creates and initializes a new DictionaryBase.
      ShortStringDictionary mySSC = new ShortStringDictionary();

      // Adds elements to the collection.
      mySSC.Add( "One", "a" );
      mySSC.Add( "Two", "ab" );
      mySSC.Add( "Three", "abc" );
      mySSC.Add( "Four", "abcd" );
      mySSC.Add( "Five", "abcde" );

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

      // Display the contents of the collection using the enumerator.
      Console.WriteLine( "Contents of the collection (using enumerator):" );
      PrintKeysAndValues2( mySSC );

      // Display the contents of the collection using the Keys property and the Item property.
      Console.WriteLine( "Initial contents of the collection (using Keys and Item):" );
      PrintKeysAndValues3( mySSC );

      // Tries to add a value that is too long.
      try  {
         mySSC.Add( "Ten", "abcdefghij" );
      }
      catch ( ArgumentException e )  {
         Console.WriteLine( e.ToString() );
      }

      // Tries to add a key that is too long.
      try  {
         mySSC.Add( "Eleven", "ijk" );
      }
      catch ( ArgumentException e )  {
         Console.WriteLine( e.ToString() );
      }

      Console.WriteLine();

      // Searches the collection with Contains.
      Console.WriteLine( "Contains \"Three\": {0}", mySSC.Contains( "Three" ) );
      Console.WriteLine( "Contains \"Twelve\": {0}", mySSC.Contains( "Twelve" ) );
      Console.WriteLine();

      // Removes an element from the collection.
      mySSC.Remove( "Two" );

      // Displays the contents of the collection.
      Console.WriteLine( "After removing \"Two\":" );
      PrintKeysAndValues1( mySSC );
   }

   // 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( ShortStringDictionary myCol )  {
      foreach ( DictionaryEntry myDE in myCol )
         Console.WriteLine( "   {0,-5} : {1}", myDE.Key, myDE.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( ShortStringDictionary myCol )  {
      DictionaryEntry myDE;
      System.Collections.IEnumerator myEnumerator = myCol.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         if ( myEnumerator.Current != null )  {
            myDE = (DictionaryEntry) myEnumerator.Current;
            Console.WriteLine( "   {0,-5} : {1}", myDE.Key, myDE.Value );
         }
      Console.WriteLine();
   }

   // Uses the Keys property and the Item property.
   public static void PrintKeysAndValues3( ShortStringDictionary myCol )  {
      ICollection myKeys = myCol.Keys;
      foreach ( String k in myKeys )
         Console.WriteLine( "   {0,-5} : {1}", k, myCol[k] );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

Contents of the collection (using foreach):
   Three : abc
   Five  : abcde
   Two   : ab
   One   : a
   Four  : abcd

Contents of the collection (using enumerator):
   Three : abc
   Five  : abcde
   Two   : ab
   One   : a
   Four  : abcd

Initial contents of the collection (using Keys and Item):
   Three : abc
   Five  : abcde
   Two   : ab
   One   : a
   Four  : abcd

System.ArgumentException: value must be no more than 5 characters in length.
Parameter name: value
   at ShortStringDictionary.OnValidate(Object key, Object value)
   at System.Collections.DictionaryBase.System.Collections.IDictionary.Add(Object key, Object value)
   at SamplesDictionaryBase.Main()
System.ArgumentException: key must be no more than 5 characters in length.
Parameter name: key
   at ShortStringDictionary.OnValidate(Object key, Object value)
   at System.Collections.DictionaryBase.System.Collections.IDictionary.Add(Object key, Object value)
   at SamplesDictionaryBase.Main()

Contains "Three": True
Contains "Twelve": False

After removing "Two":
   Three : abc
   Five  : abcde
   One   : a
   Four  : abcd

*/
Imports System.Collections

Public Class ShortStringDictionary
   Inherits DictionaryBase

   Default Public Property Item(key As String) As String
      Get
         Return CType(Dictionary(key), String)
      End Get
      Set
         Dictionary(key) = value
      End Set
   End Property

   Public ReadOnly Property Keys() As ICollection
      Get
         Return Dictionary.Keys
      End Get
   End Property

   Public ReadOnly Property Values() As ICollection
      Get
         Return Dictionary.Values
      End Get
   End Property

   Public Sub Add(key As String, value As String)
      Dictionary.Add(key, value)
   End Sub

   Public Function Contains(key As String) As Boolean
      Return Dictionary.Contains(key)
   End Function 'Contains

   Public Sub Remove(key As String)
      Dictionary.Remove(key)
   End Sub

   Protected Overrides Sub OnInsert(key As Object, value As Object)
      If Not GetType(System.String).IsAssignableFrom(key.GetType()) Then
         Throw New ArgumentException("key must be of type String.", "key")
      Else
         Dim strKey As String = CType(key, String)
         If strKey.Length > 5 Then
            Throw New ArgumentException("key must be no more than 5 characters in length.", "key")
         End If
      End If 
      If Not GetType(System.String).IsAssignableFrom(value.GetType()) Then
         Throw New ArgumentException("value must be of type String.", "value")
      Else
         Dim strValue As String = CType(value, String)
         If strValue.Length > 5 Then
            Throw New ArgumentException("value must be no more than 5 characters in length.", "value")
         End If
      End If
   End Sub

   Protected Overrides Sub OnRemove(key As Object, value As Object)
      If Not GetType(System.String).IsAssignableFrom(key.GetType()) Then
         Throw New ArgumentException("key must be of type String.", "key")
      Else
         Dim strKey As String = CType(key, String)
         If strKey.Length > 5 Then
            Throw New ArgumentException("key must be no more than 5 characters in length.", "key")
         End If
      End If
   End Sub

   Protected Overrides Sub OnSet(key As Object, oldValue As Object, newValue As Object)
      If Not GetType(System.String).IsAssignableFrom(key.GetType()) Then
         Throw New ArgumentException("key must be of type String.", "key")
      Else
         Dim strKey As String = CType(key, String)
         If strKey.Length > 5 Then
            Throw New ArgumentException("key must be no more than 5 characters in length.", "key")
         End If
      End If 
      If Not GetType(System.String).IsAssignableFrom(newValue.GetType()) Then
         Throw New ArgumentException("newValue must be of type String.", "newValue")
      Else
         Dim strValue As String = CType(newValue, String)
         If strValue.Length > 5 Then
            Throw New ArgumentException("newValue must be no more than 5 characters in length.", "newValue")
         End If
      End If
   End Sub

   Protected Overrides Sub OnValidate(key As Object, value As Object)
      If Not GetType(System.String).IsAssignableFrom(key.GetType()) Then
         Throw New ArgumentException("key must be of type String.", "key")
      Else
         Dim strKey As String = CType(key, String)
         If strKey.Length > 5 Then
            Throw New ArgumentException("key must be no more than 5 characters in length.", "key")
         End If
      End If 
      If Not GetType(System.String).IsAssignableFrom(value.GetType()) Then
         Throw New ArgumentException("value must be of type String.", "value")
      Else
         Dim strValue As String = CType(value, String)
         If strValue.Length > 5 Then
            Throw New ArgumentException("value must be no more than 5 characters in length.", "value")
         End If
      End If
   End Sub

End Class


Public Class SamplesDictionaryBase

   Public Shared Sub Main()

      ' Creates and initializes a new DictionaryBase.
      Dim mySSC As New ShortStringDictionary()

      ' Adds elements to the collection.
      mySSC.Add("One", "a")
      mySSC.Add("Two", "ab")
      mySSC.Add("Three", "abc")
      mySSC.Add("Four", "abcd")
      mySSC.Add("Five", "abcde")

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

      ' Display the contents of the collection using the enumerator.
      Console.WriteLine("Contents of the collection (using enumerator):")
      PrintKeysAndValues2(mySSC)

      ' Display the contents of the collection using the Keys property and the Item property.
      Console.WriteLine("Initial contents of the collection (using Keys and Item):")
      PrintKeysAndValues3(mySSC)

      ' Tries to add a value that is too long.
      Try
          mySSC.Add("Ten", "abcdefghij")
      Catch e As ArgumentException
          Console.WriteLine(e.ToString())
      End Try

      ' Tries to add a key that is too long.
      Try
          mySSC.Add("Eleven", "ijk")
      Catch e As ArgumentException
          Console.WriteLine(e.ToString())
      End Try

      Console.WriteLine()

      ' Searches the collection with Contains.
      Console.WriteLine("Contains ""Three"": {0}", mySSC.Contains("Three"))
      Console.WriteLine("Contains ""Twelve"": {0}", mySSC.Contains("Twelve"))
      Console.WriteLine()

      ' Removes an element from the collection.
      mySSC.Remove("Two")

      ' Displays the contents of the collection.
      Console.WriteLine("After removing ""Two"":")
      PrintKeysAndValues1(mySSC)

    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 ShortStringDictionary)
      Dim myDE As DictionaryEntry
      For Each myDE In  myCol
          Console.WriteLine("   {0,-5} : {1}", myDE.Key, myDE.Value)
      Next myDE
      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 ShortStringDictionary)
      Dim myDE As DictionaryEntry
      Dim myEnumerator As System.Collections.IEnumerator = myCol.GetEnumerator()
      While myEnumerator.MoveNext()
          If Not (myEnumerator.Current Is Nothing) Then
            myDE = CType(myEnumerator.Current, DictionaryEntry)
            Console.WriteLine("   {0,-5} : {1}", myDE.Key, myDE.Value)
          End If
      End While
      Console.WriteLine()
    End Sub


    ' Uses the Keys property and the Item property.
    Public Shared Sub PrintKeysAndValues3(myCol As ShortStringDictionary)
      Dim myKeys As ICollection = myCol.Keys
      Dim k As String
      For Each k In  myKeys
          Console.WriteLine("   {0,-5} : {1}", k, myCol(k))
      Next k
      Console.WriteLine()
    End Sub

End Class


'This code produces the following output.
'
'Contents of the collection (using For Each):
'   Three : abc
'   Five  : abcde
'   Two   : ab
'   One   : a
'   Four  : abcd
'
'Contents of the collection (using enumerator):
'   Three : abc
'   Five  : abcde
'   Two   : ab
'   One   : a
'   Four  : abcd
'
'Initial contents of the collection (using Keys and Item):
'   Three : abc
'   Five  : abcde
'   Two   : ab
'   One   : a
'   Four  : abcd
'
'System.ArgumentException: value must be no more than 5 characters in length.
'Parameter name: value
'   at ShortStringDictionary.OnValidate(Object key, Object value)
'   at System.Collections.DictionaryBase.System.Collections.IDictionary.Add(Object key, Object value)
'   at SamplesDictionaryBase.Main()
'System.ArgumentException: key must be no more than 5 characters in length.
'Parameter name: key
'   at ShortStringDictionary.OnValidate(Object key, Object value)
'   at System.Collections.DictionaryBase.System.Collections.IDictionary.Add(Object key, Object value)
'   at SamplesDictionaryBase.Main()
'
'Contains "Three": True
'Contains "Twelve": False
'
'After removing "Two":
'   Three : abc
'   Five  : abcde
'   One   : a
'   Four  : abcd

Hinweise

Wichtig

Es wird nicht empfohlen, die DictionaryBase -Klasse für die Neuentwicklung zu verwenden. Stattdessen wird empfohlen, die generische Dictionary<TKey,TValue> - oder KeyedCollection<TKey,TItem> -Klasse zu verwenden. Weitere Informationen finden Sie unter Nicht generische Auflistungen sollten nicht auf GitHub verwendet werden.

Die C#- foreach-Anweisung und die Visual Basic For Each-Anweisung geben ein Objekt vom Typ der Elemente in der Auflistung zurück. Da jedes Element von DictionaryBase ein Schlüssel-Wert-Paar ist, ist der Elementtyp nicht der Typ des Schlüssels oder der Typ des Werts. Stattdessen ist DictionaryEntryder Elementtyp .

Die foreach -Anweisung ist ein Wrapper um den Enumerator, der nur das Lesen und nicht das Schreiben in die Auflistung zulässt.

Hinweis

Da Schlüssel vererbt und ihr Verhalten geändert werden kann, kann ihre absolute Eindeutigkeit nicht durch Vergleiche mit der Equals -Methode garantiert werden.

Hinweise für Ausführende

Diese Basisklasse wird bereitgestellt, um Implementierungen das Erstellen einer stark typisierten benutzerdefinierten Auflistung zu erleichtern. Implementierer werden empfohlen, diese Basisklasse zu erweitern, anstatt eine eigene zu erstellen.

Member dieser Basisklasse sind geschützt und sollen nur über eine abgeleitete Klasse verwendet werden.

Konstruktoren

DictionaryBase()

Initialisiert eine neue Instanz der DictionaryBase-Klasse.

Eigenschaften

Count

Ruft die Anzahl der in der DictionaryBase-Instanz enthaltenen Elemente ab.

Dictionary

Ruft die Liste der in der DictionaryBase-Instanz enthaltenen Elemente ab.

InnerHashtable

Ruft die Liste der in der DictionaryBase-Instanz enthaltenen Elemente ab.

Methoden

Clear()

Löscht den Inhalt der DictionaryBase-Instanz.

CopyTo(Array, Int32)

Kopiert die DictionaryBase-Elemente am angegebenen Index in ein eindimensionales Array.

Equals(Object)

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

(Geerbt von Object)
GetEnumerator()

Gibt einen IDictionaryEnumerator zurück, der die DictionaryBase-Instanz 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)
OnClear()

Führt vor dem Löschen des Inhalts der DictionaryBase-Instanz zusätzliche benutzerdefinierte Prozesse aus.

OnClearComplete()

Führt nach dem Löschen des Inhalts der DictionaryBase-Instanz zusätzliche benutzerdefinierte Prozesse aus.

OnGet(Object, Object)

Ruft das Element mit dem angegebenen Schlüssel und Wert aus der DictionaryBase-Instanz ab.

OnInsert(Object, Object)

Führt zusätzliche benutzerdefinierte Prozesse vor dem Einfügen eines neuen Elements in die DictionaryBase-Instanz aus.

OnInsertComplete(Object, Object)

Führt zusätzliche benutzerdefinierte Prozesse nach dem Einfügen eines neuen Elements in die DictionaryBase-Instanz aus.

OnRemove(Object, Object)

Führt zusätzliche benutzerdefinierte Prozesse vor dem Entfernen eines Elements aus der DictionaryBase-Instanz aus.

OnRemoveComplete(Object, Object)

Führt zusätzliche benutzerdefinierte Prozesse nach dem Entfernen eines Elements aus der DictionaryBase-Instanz aus.

OnSet(Object, Object, Object)

Führt zusätzliche benutzerdefinierte Prozesse vor dem Festlegen eines Werts in der DictionaryBase-Instanz aus.

OnSetComplete(Object, Object, Object)

Führt zusätzliche benutzerdefinierte Prozesse nach dem Festlegen eines Werts in der DictionaryBase-Instanz aus.

OnValidate(Object, Object)

Führt bei der Überprüfung des Elements mit dem angegebenen Schlüssel und Wert zusätzliche benutzerdefinierte Aktionen aus.

ToString()

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

(Geerbt von Object)

Explizite Schnittstellenimplementierungen

ICollection.IsSynchronized

Ruft einen Wert ab, der angibt, ob der Zugriff auf ein DictionaryBase-Objekt synchronisiert (threadsicher) ist.

ICollection.SyncRoot

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

IDictionary.Add(Object, Object)

Fügt dem DictionaryBase ein Element mit dem angegebenen Schlüssel und Wert hinzu.

IDictionary.Contains(Object)

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

IDictionary.IsFixedSize

Ruft einen Wert ab, der angibt, ob ein DictionaryBase-Objekt eine feste Größe hat.

IDictionary.IsReadOnly

Ruft einen Wert ab, der angibt, ob ein DictionaryBase-Objekt schreibgeschützt ist.

IDictionary.Item[Object]

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

IDictionary.Keys

Ruft ein ICollection-Objekt ab, das die Schlüssel im DictionaryBase-Objekt enthält.

IDictionary.Remove(Object)

Entfernt das Element mit dem angegebenen Schlüssel aus dem DictionaryBase.

IDictionary.Values

Ruft ein ICollection-Objekt ab, das die Werte des DictionaryBase-Objekts enthält.

IEnumerable.GetEnumerator()

Gibt einen IEnumerator zurück, der DictionaryBase 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 (Shared in Visual Basic) Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Diese Implementierung stellt keinen synchronisierten (threadsicheren) Wrapper für ein bereit DictionaryBase, aber abgeleitete Klassen können ihre eigenen synchronisierten Versionen von mithilfe der DictionaryBaseSyncRoot -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.

Weitere Informationen