DictionaryBaseとは? わかりやすく解説

DictionaryBase クラス

厳密に指定されキー/値ペアコレクションabstract 基本クラス提供します

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public MustInherit Class
 DictionaryBase
    Implements IDictionary, ICollection, IEnumerable
Dim instance As DictionaryBase
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public abstract class DictionaryBase : IDictionary,
 ICollection, IEnumerable
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public ref class DictionaryBase abstract :
 IDictionary, ICollection, IEnumerable
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public abstract class DictionaryBase implements
 IDictionary, ICollection, 
    IEnumerable
SerializableAttribute 
ComVisibleAttribute(true) 
public abstract class DictionaryBase implements
 IDictionary, ICollection, 
    IEnumerable
解説解説
使用例使用例

DictionaryBase クラス実装し、その実装を使用してLength が 5 文字以下の String キーと値のディクショナリを作成する方法については、次のコード例参照してください

Imports System
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 'Add

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

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

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

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

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

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


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


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


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

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

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

*/

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

*/
import System.*;
import System.Collections.*;

public class ShortStringDictionary extends
 DictionaryBase
{
    /** @property 
     */
    public String get_Value(String key)
    {
        return((String)(get_Dictionary().get_Item(key)));
    } //get_Value

    /** @property 
     */
    public void set_Value(String key,String
 value)
    {
        get_Dictionary().set_Item(key, value);
    } //set_Value
   
   
    /** @property 
     */
    public ICollection get_Keys()
    {
        return get_Dictionary().get_Keys();
    } //get_Keys
   
    /** @property 
     */
    public ICollection get_Values()
    {
        return get_Dictionary().get_Values();
    } //get_Values
   
    public void Add(String key, String value)
 
    {
        get_Dictionary().Add(key, value);
    } //Add
   
    public boolean Contains(String key) 
    {
        return get_Dictionary().Contains(key);
    } //Contains
   
    public void Remove(String key) 
    {
        get_Dictionary().Remove(key);
    } //Remove
   
    protected void OnInsert(Object key, Object
 value) 
    {
        if (!key.GetType().Equals(Type.GetType("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().Equals(Type.GetType("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");
            }
        }
    } //OnInsert
   
    protected void OnRemove(Object key, Object
 value) 
    {
        if (!key.GetType().Equals(Type.GetType("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");
            }
        }
    } //OnRemove
   
    protected void OnSet(Object key, Object
 oldValue, Object newValue) 
    {
        if (!key.GetType().Equals(Type.GetType("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().Equals(Type.GetType("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")
 ;
            }
        }
    } //OnSet
   
    protected void OnValidate(Object key, Object
 value) 
    {
        if (!key.GetType().Equals(Type.GetType("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().Equals(Type.GetType("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")
 ;
            }
        }
    } //OnValidate
} //ShortStringDictionary

public class SamplesDictionaryBase
{
    public static void main(String[]
 args)
    {
        // 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 while. This
 is the
        // preferred method.
        Console.WriteLine("Contents of the collection (using
 for):");
        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.
        boolean b = mySSC.Contains("Three");
        Console.Write("Contains \"Three\": ");
        Console.WriteLine(b);
        b = mySSC.Contains("Twelve");
        Console.Write("Contains \"Twelve\": ");
        Console.WriteLine(b);
        Console.WriteLine();
    
        // Removes an element from the collection.
        mySSC.Remove("Two");
    
        // Displays the contents of the collection.
        Console.WriteLine("After removing \"Two\":");
        PrintKeysAndValues1(mySSC);
    }//main
   
    // Uses the for statement which hides the complexity of the enumerator.
    // NOTE: The for statement is the preferred way of enumerating the
 
    // contents of a collection.
    public static void PrintKeysAndValues1(ShortStringDictionary
 myCol) 
    {
        String strValue;
        String strKeys[] = new String[myCol.get_Count()];
        myCol.get_Keys().CopyTo(strKeys,0);

        for (int iCtr = 0; iCtr < myCol.get_Count();
 iCtr++){
            strValue = myCol.get_Value(strKeys[iCtr]);
            Console.WriteLine("   {0,-5} : {1}", strKeys[iCtr], strValue);
        }
        Console.WriteLine();
    } //PrintKeysAndValues1
   
    // Uses the enumerator. 
    // NOTE: The for 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.get_Current()  != null
  ) {
                myDE =((DictionaryEntry)(myEnumerator.get_Current()));
                Console.WriteLine("   {0,-5} : {1}", myDE.get_Key(), 
                myDE.get_Value());
            }
        }
        Console.WriteLine();
    } //PrintKeysAndValues2
   
    // Uses the Keys property and the Value property.
    public static void PrintKeysAndValues3(ShortStringDictionary
 myCol) 
    {
        ICollection myKeys = myCol.get_Keys();
        IEnumerator myEnumerator = myKeys.GetEnumerator();
        while (myEnumerator.MoveNext()) {
            String k = myEnumerator.get_Current().ToString();
            Console.WriteLine("   {0,-5} : {1}", k, myCol.get_Item(k));
        }
        Console.WriteLine();
    } //PrintKeysAndValues3
} //SamplesDictionaryBase
 
/* 
This code produces the following output.

Contents of the collection (using while):
   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

*/

継承階層継承階層
System.Object
  System.Collections.DictionaryBase
     派生クラス
スレッド セーフスレッド セーフ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DictionaryBase メンバ
System.Collections 名前空間
System.Collections.Hashtable
System.Collections.IDictionary
System.Collections.Generic
その他の技術情報
カルチャを認識しい文字操作実行

DictionaryBase コンストラクタ


DictionaryBase プロパティ


パブリック プロパティパブリック プロパティ

プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ Dictionary DictionaryBase インスタンス格納されている要素リスト取得します
プロテクト プロパティ InnerHashtable DictionaryBase インスタンス格納されている要素リスト取得します
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.ICollection.IsSynchronized DictionaryBase オブジェクトへのアクセス同期されている (スレッド セーフである) かどうかを示す値を取得します
インターフェイスの明示的な実装 System.Collections.ICollection.SyncRoot DictionaryBase オブジェクトへのアクセス同期するために使用できるオブジェクト取得します
インターフェイスの明示的な実装 System.Collections.IDictionary.IsFixedSize DictionaryBase オブジェクト固定サイズかどうかを示す値を取得します
インターフェイスの明示的な実装 System.Collections.IDictionary.IsReadOnly DictionaryBase オブジェクト読み取り専用かどうかを示す値を取得します
インターフェイスの明示的な実装 System.Collections.IDictionary.Item 指定したキー関連付けられている値を取得または設定します
インターフェイスの明示的な実装 System.Collections.IDictionary.Keys DictionaryBase オブジェクト内のキー格納している ICollection オブジェクト取得します
インターフェイスの明示的な実装 System.Collections.IDictionary.Values DictionaryBase オブジェクト内の値を格納している ICollection オブジェクト取得します
参照参照

関連項目

DictionaryBase クラス
System.Collections 名前空間
System.Collections.Hashtable
System.Collections.IDictionary
System.Collections.Generic

その他の技術情報

カルチャを認識しい文字操作実行

DictionaryBase メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
  名前 説明
プロテクト メソッド Finalize  Objectガベージ コレクションにより収集される前に、その Objectリソース解放しその他のクリーンアップ操作実行できるようにします。 ( Object から継承されます。)
プロテクト メソッド MemberwiseClone  現在の Object簡易コピー作成します。 ( Object から継承されます。)
プロテクト メソッド OnClear DictionaryBase インスタンス内容消去する前に追加カスタム プロセス実行します
プロテクト メソッド OnClearComplete DictionaryBase インスタンス内容消去した後に、追加カスタム プロセス実行します
プロテクト メソッド OnGet 指定したキーおよび値を持つ、DictionaryBase インスタンス要素取得します
プロテクト メソッド OnInsert DictionaryBase インスタンス新し要素挿入する前に追加カスタム プロセス実行します
プロテクト メソッド OnInsertComplete DictionaryBase インスタンス新し要素挿入した後に、追加カスタム プロセス実行します
プロテクト メソッド OnRemove DictionaryBase インスタンスか要素削除する前に追加カスタム プロセス実行します
プロテクト メソッド OnRemoveComplete DictionaryBase インスタンスか要素削除した後に、追加カスタム プロセス実行します
プロテクト メソッド OnSet DictionaryBase インスタンスに値を設定する前に追加カスタム プロセス実行します
プロテクト メソッド OnSetComplete DictionaryBase インスタンスに値を設定した後に、追加カスタム プロセス実行します
プロテクト メソッド OnValidate 指定したキーおよび値を持つ要素検証するときに、追加カスタム プロセス実行します
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IDictionary.Add 指定したキーおよび値を持つ要素DictionaryBase追加します
インターフェイスの明示的な実装 System.Collections.IDictionary.Contains DictionaryBase特定のキー格納されているかどうか判断します
インターフェイスの明示的な実装 System.Collections.IDictionary.Remove 指定したキー持つ要素DictionaryBase から削除します
インターフェイスの明示的な実装 System.Collections.IEnumerable.GetEnumerator DictionaryBase反復処理する IEnumerator を返します
参照参照

関連項目

DictionaryBase クラス
System.Collections 名前空間
System.Collections.Hashtable
System.Collections.IDictionary
System.Collections.Generic

その他の技術情報

カルチャを認識しい文字操作実行

DictionaryBase メンバ

厳密に指定されキー/値ペアコレクションabstract 基本クラス提供します

DictionaryBase データ型公開されるメンバを以下の表に示します


プロテクト コンストラクタプロテクト コンストラクタ
  名前 説明
プロテクト メソッド DictionaryBase DictionaryBase クラス新しインスタンス初期化します。
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
  名前 説明
プロテクト プロパティ Dictionary DictionaryBase インスタンス格納されている要素リスト取得します
プロテクト プロパティ InnerHashtable DictionaryBase インスタンス格納されている要素リスト取得します
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
  名前 説明
プロテクト メソッド Finalize  Objectガベージ コレクションにより収集される前に、その Objectリソース解放しその他のクリーンアップ操作実行できるようにします。 (Object から継承されます。)
プロテクト メソッド MemberwiseClone  現在の Object簡易コピー作成します。 (Object から継承されます。)
プロテクト メソッド OnClear DictionaryBase インスタンス内容消去する前に追加カスタム プロセス実行します
プロテクト メソッド OnClearComplete DictionaryBase インスタンス内容消去した後に、追加カスタム プロセス実行します
プロテクト メソッド OnGet 指定したキーおよび値を持つ、DictionaryBase インスタンス要素取得します
プロテクト メソッド OnInsert DictionaryBase インスタンス新し要素挿入する前に追加カスタム プロセス実行します
プロテクト メソッド OnInsertComplete DictionaryBase インスタンス新し要素挿入した後に、追加カスタム プロセス実行します
プロテクト メソッド OnRemove DictionaryBase インスタンスか要素削除する前に追加カスタム プロセス実行します
プロテクト メソッド OnRemoveComplete DictionaryBase インスタンスか要素削除した後に、追加カスタム プロセス実行します
プロテクト メソッド OnSet DictionaryBase インスタンスに値を設定する前に追加カスタム プロセス実行します
プロテクト メソッド OnSetComplete DictionaryBase インスタンスに値を設定した後に、追加カスタム プロセス実行します
プロテクト メソッド OnValidate 指定したキーおよび値を持つ要素検証するときに、追加カスタム プロセス実行します
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IDictionary.Add 指定したキーおよび値を持つ要素DictionaryBase追加します
インターフェイスの明示的な実装 System.Collections.IDictionary.Contains DictionaryBase特定のキー格納されているかどうか判断します
インターフェイスの明示的な実装 System.Collections.IDictionary.Remove 指定したキー持つ要素DictionaryBase から削除します
インターフェイスの明示的な実装 System.Collections.IEnumerable.GetEnumerator DictionaryBase反復処理する IEnumerator を返します
インターフェイスの明示的な実装 System.Collections.ICollection.IsSynchronized DictionaryBase オブジェクトへのアクセス同期されている (スレッド セーフである) かどうかを示す値を取得します
インターフェイスの明示的な実装 System.Collections.ICollection.SyncRoot DictionaryBase オブジェクトへのアクセス同期するために使用できるオブジェクト取得します
インターフェイスの明示的な実装 System.Collections.IDictionary.IsFixedSize DictionaryBase オブジェクト固定サイズかどうかを示す値を取得します
インターフェイスの明示的な実装 System.Collections.IDictionary.IsReadOnly DictionaryBase オブジェクト読み取り専用かどうかを示す値を取得します
インターフェイスの明示的な実装 System.Collections.IDictionary.Item 指定したキー関連付けられている値を取得または設定します
インターフェイスの明示的な実装 System.Collections.IDictionary.Keys DictionaryBase オブジェクト内のキー格納している ICollection オブジェクト取得します
インターフェイスの明示的な実装 System.Collections.IDictionary.Values DictionaryBase オブジェクト内の値を格納している ICollection オブジェクト取得します
参照参照

関連項目

DictionaryBase クラス
System.Collections 名前空間
System.Collections.Hashtable
System.Collections.IDictionary
System.Collections.Generic

その他の技術情報

カルチャを認識しい文字操作実行



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DictionaryBase」の関連用語

DictionaryBaseのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DictionaryBaseのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS