DictionaryBase Class Provides the abstract base class for a strongly typed collection of key/value pairs. : DictionaryBase « Collections Data Structure « C# / C Sharp






DictionaryBase Class Provides the abstract base class for a strongly typed collection of key/value pairs.

 

using System;
using System.Collections;

public class MyDictionary : 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" );
      

      if ( value.GetType() != typeof(System.String) )
         throw new ArgumentException( "value must be of type String.", "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" );
      
   }

   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" );
      

      if ( newValue.GetType() != typeof(System.String) )
         throw new ArgumentException( "newValue must be of type String.", "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" );
      

      if ( value.GetType() != typeof(System.String) )
         throw new ArgumentException( "value must be of type String.", "value" );
      
   }

}


public class SamplesDictionaryBase  {

   public static void Main()  {
      MyDictionary mySSC = new MyDictionary();
      mySSC.Add( "One", "a" );
      mySSC.Add( "Two", "ab" );
      PrintKeysAndValues1( mySSC );

      PrintKeysAndValues2( mySSC );

      PrintKeysAndValues3( mySSC );
   }
   public static void PrintKeysAndValues1( MyDictionary myCol )  {
      foreach ( DictionaryEntry myDE in myCol )
         Console.WriteLine( "   {0,-5} : {1}", myDE.Key, myDE.Value );
   }
   public static void PrintKeysAndValues2( MyDictionary 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 );
         }
   }
   public static void PrintKeysAndValues3( MyDictionary myCol )  {
      ICollection myKeys = myCol.Keys;
      foreach ( String k in myKeys )
         Console.WriteLine( "   {0,-5} : {1}", k, myCol[k] );
   }
}

   
  








Related examples in the same category

1.extends DictionaryBase
2.Add a list of items into the dictionary.
3.Copy/Clone all the element from a dictionary to another.
4.Put an element into the Dictionary
5.Add one dictionary items into another. Copy all the first dictionary's items into the second dictionary.
6.A dictionary with keys of type string and values of type String
7.A dictionary with keys of type string and values of type Type
8.Dictionary to anonymous type
9.Two Key Dictionary