using System.Collections;
public class ShortStringDictionary : DictionaryBase {
public String this[ String key ] {
return( (String) Dictionary[key] );
public ICollection Keys {
return( Dictionary.Keys );
public ICollection Values {
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" );
String strKey = (String) key;
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" );
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" );
String strKey = (String) key;
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" );
String strKey = (String) key;
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" );
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" );
String strKey = (String) key;
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" );
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() {
ShortStringDictionary mySSC = new ShortStringDictionary();
mySSC.Add( "Two", "ab" );
mySSC.Add( "Three", "abc" );
mySSC.Add( "Four", "abcd" );
mySSC.Add( "Five", "abcde" );
Console.WriteLine( "Contents of the collection (using foreach):" );
PrintKeysAndValues1( mySSC );
Console.WriteLine( "Contents of the collection (using enumerator):" );
PrintKeysAndValues2( mySSC );
Console.WriteLine( "Initial contents of the collection (using Keys and Item):" );
PrintKeysAndValues3( mySSC );
mySSC.Add( "Ten", "abcdefghij" );
catch ( ArgumentException e ) {
Console.WriteLine( e.ToString() );
mySSC.Add( "Eleven", "ijk" );
catch ( ArgumentException e ) {
Console.WriteLine( e.ToString() );
Console.WriteLine( "Contains \"Three\": {0}", mySSC.Contains( "Three" ) );
Console.WriteLine( "Contains \"Twelve\": {0}", mySSC.Contains( "Twelve" ) );
Console.WriteLine( "After removing \"Two\":" );
PrintKeysAndValues1( mySSC );
public static void PrintKeysAndValues1( ShortStringDictionary myCol ) {
foreach ( DictionaryEntry myDE in myCol )
Console.WriteLine( " {0,-5} : {1}", myDE.Key, myDE.Value );
public static void PrintKeysAndValues2( ShortStringDictionary myCol ) {
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( ShortStringDictionary myCol ) {
ICollection myKeys = myCol.Keys;
foreach ( String k in myKeys )
Console.WriteLine( " {0,-5} : {1}", k, myCol[k] );