Provides the abstract base class for a strongly typed collection of key-and-value pairs.
public abstract class DictionaryBase: Object, IDictionary
ObjectDictionaryBase
// This examples shows how to implement a DictionaryBase.
// It shows also some properties and method of the DictionaryBase class.
using System;
using System.Collections;
using System.Diagnostics;
public class SampleDictionaryBase
{
static void Main()
{
// Creates and initializes a new DictionaryBase.
MyDictionary myDict = new MyDictionary();
// Adds elements to the collection.
myDict.Add( 1, "a" );
myDict.Add( 2, "ab" );
myDict.Add( 3, "abc" );
myDict.Add( 4, "abcd" );
myDict.Add( 5, "abcde" );
// Displays the contents of the collection.
Debug.WriteLine("Initial contents of the collection:");
DisplayValues3(myDict
);
// Searches the collection with Contains.
Debug.WriteLine(@"Contains ""3"":" +myDict.Contains( 3 ).ToString());
Debug.WriteLine(@"Contains ""12"":" +myDict.Contains( 12 ).ToString());
// Creates and displays the content of an array.
int[] myIntArray = new int[5] ;
Debug.Write("\nContents of an array of int:");
DisplayArrayValues(myIntArray);
// Copies the keys of the collection to the array of int.
myDict.Keys.CopyTo(myIntArray,0);
Debug.Write("\n\nContents of the array after copying the keys of the collection in it:");
DisplayArrayValues(myIntArray);
// Removes an element from the collection.
myDict.Remove( 2 );
Debug.WriteLine("\n\n"+@"Contents of the collection after removing ""2"":");
DisplayValues3(myDict);
}
// Displays values using Keys property and the this[] method.
public static void DisplayValues( MyDictionary myDictionary )
{
ICollection myKeys = myDictionary.Keys;
foreach ( int k in myKeys )
Debug.WriteLine("\t"+k+":"+myDictionary[k].ToString());
}
// Displays values using an enumerator.
public static void DisplayValues2( MyDictionary myDictionary )
{
DictionaryEntry myDE;
System.Collections.IEnumerator myEnumerator = myDictionary.GetEnumerator();
while ( myEnumerator.MoveNext() )
if ( myEnumerator.Current != null )
myDE = (DictionaryEntry) myEnumerator.Current;
Debug.WriteLine("\t"+myDE.Key.ToString()+":"+myDE.Value.ToString());
}
// Displays values using the foreach statement.
public static void DisplayValues3 ( MyDictionary myDictionary)
{
foreach ( DictionaryEntry myDE in myDictionary )
Debug.WriteLine("\t"+myDE.Key.ToString()+":"+myDE.Value.ToString());
}
// Displays the values of an array.
public static void DisplayArrayValues ( Array array )
{
for ( int i = 0; i < array.Length; i++ )
{
Debug.Write(array.GetValue(i)+" ");
}
}
}
public class MyDictionary : DictionaryBase
{
public string this[ int 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( int key, String value )
{
Dictionary.Add( key, value );
}
public bool Contains( int key )
{
return( Dictionary.Contains( key ) );
}
public void Remove( int key )
{
Dictionary.Remove( key );
}
protected override void OnInsert( Object key, Object value )
{
// Insert additional code to be executed only when inserting values.
}
protected override void OnRemove( Object key, Object value )
{
// Insert additional code to be executed only when removing values.
}
protected override void OnSet( Object key, Object oldValue, Object newValue )
{
// Insert additional code to be executed only when setting values.
}
}
/*
The output of the sample.
Initial contents of the collection:
5:abcde
1:a
3:abc
2:ab
4:abcd
Contains "3":True
Contains "12":False
Contents of an array of int:0 0 0 0 0
Contents of the array after copying the keys of the collection in it:4 2 3 5 1
Contents of the collection after removing 2:
5:abcde
1:a
3:abc
4:abcd
*/
Namespace System.Collections Flash Library corlib.scl Flash Library Version 2.0.0.2466 Silverlight Library System.Windows.Forms.dll
| © 2003-2007 NETiKA Technologies. All rights reserved. |