DictionaryBase DictionaryBase DictionaryBase DictionaryBase Class

定義

厳密に型指定されたキー/値ペアのコレクションの abstract 基本クラスを提供します。Provides the abstract base class for a strongly typed collection of key/value pairs.

public ref class DictionaryBase abstract : System::Collections::IDictionary
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public abstract class DictionaryBase : System.Collections.IDictionary
type DictionaryBase = class
    interface IDictionary
    interface ICollection
    interface IEnumerable
Public MustInherit Class DictionaryBase
Implements IDictionary
継承
DictionaryBaseDictionaryBaseDictionaryBaseDictionaryBase
派生
属性
実装

次のコード例ではDictionaryBase 、クラスを実装し、その実装を使用Stringして、5文字以下Lengthのを持つキーと値のディクショナリを作成します。The following code example implements the DictionaryBase class and uses that implementation to create a dictionary of String keys and values that have a Length of 5 characters or less.

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 'ShortStringDictionary


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 'SamplesDictionaryBase 


'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

注釈

重要

新しい開発にはDictionaryBaseクラスを使用しないことをお勧めします。We don't recommend that you use the DictionaryBase class for new development. 代わりに、ジェネリックDictionary<TKey,TValue>またはKeyedCollection<TKey,TItem>クラスを使用することをお勧めします。Instead, we recommend that you use the generic Dictionary<TKey,TValue> or KeyedCollection<TKey,TItem> class . 詳細については、「GitHub で非ジェネリックコレクションを使用することはできません。」を参照してください。For more information, see Non-generic collections shouldn't be used on GitHub.

C# Foreachステートメントと各ステートメントのVisual Basic は、コレクション内の要素の型のオブジェクトを返します。The C# foreach statement and the Visual Basic For Each statement return an object of the type of the elements in the collection. の各要素DictionaryBaseはキーと値のペアであるため、要素の型はキーの型でも、値の型でもありません。Since each element of the DictionaryBase is a key/value pair, the element type is not the type of the key or the type of the value. 代わりに、要素の型はDictionaryEntryです。Instead, the element type is DictionaryEntry.

foreachステートメントは、列挙子のラッパーであり、コレクションへの書き込みではなく読み取りのみが許可されます。The foreach statement is a wrapper around the enumerator, which only allows reading from, not writing to, the collection.

注意

キーを継承してその動作を変更できるため、 Equalsメソッドを使用して比較することで、それらの絶対一意性を保証することはできません。Because keys can be inherited and their behavior changed, their absolute uniqueness cannot be guaranteed by comparisons using the Equals method.

注意 (継承者)

この基底クラスは、実装者が厳密に型指定されたカスタムコレクションを簡単に作成できるようにするために用意されています。This base class is provided to make it easier for implementers to create a strongly typed custom collection. 実装者は、独自の基底クラスを作成するのではなく、この基本クラスを拡張することをお勧めします。Implementers are encouraged to extend this base class instead of creating their own.

この基底クラスのメンバーは保護されており、派生クラスでのみ使用されることを意図しています。Members of this base class are protected and are intended to be used through a derived class only.

コンストラクター

DictionaryBase() DictionaryBase() DictionaryBase() DictionaryBase()

DictionaryBase クラスの新しいインスタンスを初期化します。Initializes a new instance of the DictionaryBase class.

プロパティ

Count Count Count Count

DictionaryBase インスタンスに格納されている要素の数を取得します。Gets the number of elements contained in the DictionaryBase instance.

Dictionary Dictionary Dictionary Dictionary

DictionaryBase インスタンスに格納されている要素のリストを取得します。Gets the list of elements contained in the DictionaryBase instance.

InnerHashtable InnerHashtable InnerHashtable InnerHashtable

DictionaryBase インスタンスに格納されている要素のリストを取得します。Gets the list of elements contained in the DictionaryBase instance.

メソッド

Clear() Clear() Clear() Clear()

DictionaryBase インスタンスの内容を消去します。Clears the contents of the DictionaryBase instance.

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

1 次元の DictionaryBase の指定したインデックスに Array の要素をコピーします。Copies the DictionaryBase elements to a one-dimensional Array at the specified index.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

指定したオブジェクトが、現在のオブジェクトと等しいかどうかを判断します。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

IDictionaryEnumerator インスタンスを反復処理する DictionaryBase を返します。Returns an IDictionaryEnumerator that iterates through the DictionaryBase instance.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(Inherited from Object)
OnClear() OnClear() OnClear() OnClear()

DictionaryBase インスタンスの内容を消去する前に、追加のカスタム プロセスを実行します。Performs additional custom processes before clearing the contents of the DictionaryBase instance.

OnClearComplete() OnClearComplete() OnClearComplete() OnClearComplete()

DictionaryBase インスタンスの内容を消去した後に、追加のカスタム プロセスを実行します。Performs additional custom processes after clearing the contents of the DictionaryBase instance.

OnGet(Object, Object) OnGet(Object, Object) OnGet(Object, Object) OnGet(Object, Object)

指定したキーおよび値を持つ、DictionaryBase インスタンスの要素を取得します。Gets the element with the specified key and value in the DictionaryBase instance.

OnInsert(Object, Object) OnInsert(Object, Object) OnInsert(Object, Object) OnInsert(Object, Object)

DictionaryBase インスタンスに新しい要素を挿入する前に、追加のカスタム プロセスを実行します。Performs additional custom processes before inserting a new element into the DictionaryBase instance.

OnInsertComplete(Object, Object) OnInsertComplete(Object, Object) OnInsertComplete(Object, Object) OnInsertComplete(Object, Object)

DictionaryBase インスタンスに新しい要素を挿入した後に、追加のカスタム プロセスを実行します。Performs additional custom processes after inserting a new element into the DictionaryBase instance.

OnRemove(Object, Object) OnRemove(Object, Object) OnRemove(Object, Object) OnRemove(Object, Object)

DictionaryBase インスタンスから要素を削除する前に、追加のカスタム プロセスを実行します。Performs additional custom processes before removing an element from the DictionaryBase instance.

OnRemoveComplete(Object, Object) OnRemoveComplete(Object, Object) OnRemoveComplete(Object, Object) OnRemoveComplete(Object, Object)

DictionaryBase インスタンスから要素を削除した後に、追加のカスタム プロセスを実行します。Performs additional custom processes after removing an element from the DictionaryBase instance.

OnSet(Object, Object, Object) OnSet(Object, Object, Object) OnSet(Object, Object, Object) OnSet(Object, Object, Object)

DictionaryBase インスタンスに値を設定する前に、追加のカスタム プロセスを実行します。Performs additional custom processes before setting a value in the DictionaryBase instance.

OnSetComplete(Object, Object, Object) OnSetComplete(Object, Object, Object) OnSetComplete(Object, Object, Object) OnSetComplete(Object, Object, Object)

DictionaryBase インスタンスに値を設定した後に、追加のカスタム プロセスを実行します。Performs additional custom processes after setting a value in the DictionaryBase instance.

OnValidate(Object, Object) OnValidate(Object, Object) OnValidate(Object, Object) OnValidate(Object, Object)

指定したキーおよび値を持つ要素を検証するときに、追加のカスタム プロセスを実行します。Performs additional custom processes when validating the element with the specified key and value.

ToString() ToString() ToString() ToString()

現在のオブジェクトを表す文字列を返します。Returns a string that represents the current object.

(Inherited from Object)

明示的なインターフェイスの実装

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

DictionaryBase オブジェクトへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。Gets a value indicating whether access to a DictionaryBase object is synchronized (thread safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

DictionaryBase オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。Gets an object that can be used to synchronize access to a DictionaryBase object.

IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object) IDictionary.Add(Object, Object)

指定したキーおよび値を持つ要素を DictionaryBase に追加します。Adds an element with the specified key and value into the DictionaryBase.

IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object) IDictionary.Contains(Object)

DictionaryBase に特定のキーが格納されているかどうかを判断します。Determines whether the DictionaryBase contains a specific key.

IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize IDictionary.IsFixedSize

DictionaryBase オブジェクトが固定サイズかどうかを示す値を取得します。Gets a value indicating whether a DictionaryBase object has a fixed size.

IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly IDictionary.IsReadOnly

DictionaryBase オブジェクトが読み取り専用かどうかを示す値を取得します。Gets a value indicating whether a DictionaryBase object is read-only.

IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object] IDictionary.Item[Object]

指定されたキーに関連付けられている値を取得または設定します。Gets or sets the value associated with the specified key.

IDictionary.Keys IDictionary.Keys IDictionary.Keys IDictionary.Keys

ICollection オブジェクト内のキーを格納している DictionaryBase オブジェクトを取得します。Gets an ICollection object containing the keys in the DictionaryBase object.

IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object) IDictionary.Remove(Object)

指定したキーを持つ要素を DictionaryBase から削除します。Removes the element with the specified key from the DictionaryBase.

IDictionary.Values IDictionary.Values IDictionary.Values IDictionary.Values

ICollection オブジェクト内の値を格納している DictionaryBase オブジェクトを取得します。Gets an ICollection object containing the values in the DictionaryBase object.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

DictionaryBase を反復処理する IEnumerator を返します。Returns an IEnumerator that iterates through the DictionaryBase.

拡張メソッド

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

クエリの並列化を有効にします。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。Converts an IEnumerable to an IQueryable.

適用対象

スレッド セーフ

パブリック静的 (Visual Basic ではShared) なこの型のメンバーはスレッド セーフですPublic static (Shared in Visual Basic) members of this type are thread safe. インスタンス メンバーの場合は、スレッド セーフであるとは限りません。Any instance members are not guaranteed to be thread safe.

この実装は、 DictionaryBaseに対して同期された (スレッドセーフな) ラッパーを提供しませんが、派生クラスはDictionaryBase SyncRootプロパティを使用して、独自の同期バージョンを作成できます。This implementation does not provide a synchronized (thread-safe) wrapper for a DictionaryBase, but derived classes can create their own synchronized versions of the DictionaryBase using the SyncRoot property.

コレクションの列挙は、本質的にスレッド セーフなプロシージャではありません。Enumerating through a collection is intrinsically not a thread-safe procedure. コレクションの同期がとられている場合でも、別のスレッドによってそのコレクションを変更できるため、変更の結果として列挙子は例外をスローします。Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

こちらもご覧ください