英語で読む

次の方法で共有


DictionaryBase クラス

定義

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

C#
public abstract class DictionaryBase : System.Collections.IDictionary
C#
[System.Serializable]
public abstract class DictionaryBase : System.Collections.IDictionary
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class DictionaryBase : System.Collections.IDictionary
継承
DictionaryBase
派生
属性
実装

次のコード例では、 クラスを DictionaryBase 実装し、その実装を使用して、 が 5 文字以下の String キーと値のディクショナリを Length 作成します。

C#
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

*/

注釈

重要

新しい開発には クラスを DictionaryBase 使用しないことをお勧めします。 代わりに、ジェネリック Dictionary<TKey,TValue> クラスまたは KeyedCollection<TKey,TItem> クラス を使用することをお勧めします。 詳細については、「GitHub で 非ジェネリック コレクションを使用しないでください 」を参照してください。

C# foreach ステートメントと Visual Basic For Each ステートメントは、コレクション内の要素の型のオブジェクトを返します。 の DictionaryBase 各要素はキーと値のペアであるため、要素の型はキーの型または値の型ではありません。 代わりに、要素の型は です DictionaryEntry

ステートメントは foreach 列挙子を囲むラッパーであり、コレクションの書き込みではなく、コレクションからの読み取りのみを許可します。

注意

キーを継承して動作を変更できるため、 メソッドを使用した比較では、その絶対一意性を Equals 保証できません。

注意 (実装者)

この基本クラスは、実装者が厳密に型指定されたカスタム コレクションを簡単に作成できるようにするために提供されます。 実装者は、独自の基底クラスを作成するのではなく、この基本クラスを拡張することをお勧めします。

この基底クラスのメンバーは保護されており、派生クラスでのみ使用することを目的としています。

コンストラクター

DictionaryBase()

DictionaryBase クラスの新しいインスタンスを初期化します。

プロパティ

Count

DictionaryBase インスタンスに含まれる要素の数を取得します。

Dictionary

DictionaryBase インスタンスに格納されている要素のリストを取得します。

InnerHashtable

DictionaryBase インスタンスに格納されている要素のリストを取得します。

メソッド

Clear()

DictionaryBase インスタンスの内容を消去します。

CopyTo(Array, Int32)

1 次元の DictionaryBase の指定したインデックスに Array の要素をコピーします。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
GetEnumerator()

IDictionaryEnumerator インスタンスを反復処理する DictionaryBase を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
OnClear()

DictionaryBase インスタンスの内容を消去する前に、追加のカスタム プロセスを実行します。

OnClearComplete()

DictionaryBase インスタンスの内容を消去した後に、追加のカスタム プロセスを実行します。

OnGet(Object, Object)

指定したキーおよび値を持つ、DictionaryBase インスタンスの要素を取得します。

OnInsert(Object, Object)

DictionaryBase インスタンスに新しい要素を挿入する前に、追加のカスタム プロセスを実行します。

OnInsertComplete(Object, Object)

DictionaryBase インスタンスに新しい要素を挿入した後に、追加のカスタム プロセスを実行します。

OnRemove(Object, Object)

DictionaryBase インスタンスから要素を削除する前に、追加のカスタム プロセスを実行します。

OnRemoveComplete(Object, Object)

DictionaryBase インスタンスから要素を削除した後に、追加のカスタム プロセスを実行します。

OnSet(Object, Object, Object)

DictionaryBase インスタンスに値を設定する前に、追加のカスタム プロセスを実行します。

OnSetComplete(Object, Object, Object)

DictionaryBase インスタンスに値を設定した後に、追加のカスタム プロセスを実行します。

OnValidate(Object, Object)

指定したキーおよび値を持つ要素を検証するときに、追加のカスタム プロセスを実行します。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

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

ICollection.IsSynchronized

DictionaryBase オブジェクトへのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

ICollection.SyncRoot

DictionaryBase オブジェクトへのアクセスを同期するために使用できるオブジェクトを取得します。

IDictionary.Add(Object, Object)

指定したキーおよび値を持つ要素を DictionaryBase に追加します。

IDictionary.Contains(Object)

DictionaryBase に特定のキーが格納されているかどうかを判断します。

IDictionary.IsFixedSize

DictionaryBase オブジェクトが固定サイズかどうかを示す値を取得します。

IDictionary.IsReadOnly

DictionaryBase オブジェクトが読み取り専用かどうかを示す値を取得します。

IDictionary.Item[Object]

指定されたキーに関連付けられている値を取得または設定します。

IDictionary.Keys

ICollection オブジェクト内のキーを格納している DictionaryBase オブジェクトを取得します。

IDictionary.Remove(Object)

指定したキーを持つ要素を DictionaryBase から削除します。

IDictionary.Values

ICollection オブジェクト内の値を格納している DictionaryBase オブジェクトを取得します。

IEnumerable.GetEnumerator()

DictionaryBase を反復処理する IEnumerator を返します。

拡張メソッド

Cast<TResult>(IEnumerable)

IEnumerable の要素を、指定した型にキャストします。

OfType<TResult>(IEnumerable)

指定された型に基づいて IEnumerable の要素をフィルター処理します。

AsParallel(IEnumerable)

クエリの並列化を有効にします。

AsQueryable(IEnumerable)

IEnumerableIQueryable に変換します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

スレッド セーフ

パブリック静的 (Visual Basic ではShared) なこの型のメンバーはスレッド セーフです インスタンス メンバーの場合は、スレッド セーフであるとは限りません。

この実装では、 の同期 (スレッド セーフ) ラッパーDictionaryBaseは提供されませんが、派生クラスでは、 プロパティを使用してSyncRoot独自の同期バージョンをDictionaryBase作成できます。

コレクションの列挙処理は、本質的にスレッドセーフな処理ではありません。 コレクションが同期されていても、他のスレッドがコレクションを変更する場合があり、このときは列挙子から例外がスローされます。 列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。

こちらもご覧ください