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] );
}
}