Provides the abstract base class for a collection of associated String keys and Object values that are organized based on the hash code of the key. This class cannot be inherited.
public sealed class NameHashtable: Object, IDictionary
ObjectNameHashtable
// This example shows how to create and initialize a NameHashtable.
// It shows also some properties and method of the NameHashtable class.
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
public class SampleNameHashtable
{
static void Main()
{
// Creates and initializes a new NameHashtable.
NameHashtable myNhash= new NameHashtable();
// Adds elements to the NameHashtable.
myNhash.Add( "Banana", 1.29 );
myNhash.Add( "Strawberry", 1.29 );
myNhash.Add( "Orange", 0.89 );
myNhash.Add( "Coconut", 0.99 );
// Displays the contents of the NameHashtable.
Debug.WriteLine("Initial contents of the NameHashtable(key:value):");
DisplayValues(myNhash
);
// Searches the NameHashtable with Contains method.
bool contain = myNhash.Contains("Coconut");
Debug.WriteLine("\n"+@"Contains ""Coconut"":" +contain.ToString());
// Removes an element of the NameHashtable.
Debug.WriteLine("\n"+@"Contents of the NameHashtable after removing the ""Coconut"" key element(key:value):");
DisplayValues2(myNhash);
// Creates and initialize an array of string.
string[] myStringArray = new string[7] {"abc", "def", "ghi", "jkl", "mno","pqr", "stu"};
Debug.Write("\nInitial contents the array of string:");
DisplayArrayValues(myStringArray);
// Copies the keys of the NameHashtable to the array.
myNhash.Keys.CopyTo(myStringArray, 0);
Debug.Write("\nContents of the array after copying the keys of the NameHashtable in it:");
DisplayArrayValues(myStringArray);
// Set the value of one element of the NameHashtable.
myNhash["Apple"]="test";
Debug.WriteLine("\n\n"+@"Contents of the NameHashtable after setting the ""Apple"" key element to ""test"":");
DisplayValues3(myNhash);
}
// Displays values using Keys property and the this[] method.
public static void DisplayValues( NameHashtable myNameHashtable )
{
ICollection myKeys = myNameHashtable.Keys;
foreach ( string k in myKeys )
Debug.WriteLine("\t"+k+":"+myNameHashtable[k].ToString());
}
// Displays values using an enumerator.
public static void DisplayValues2( NameHashtable myNameHashtable )
{
DictionaryEntry myDE;
System.Collections.IEnumerator myEnumerator = myNameHashtable.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 ( NameHashtable myNameHashtable )
{
foreach ( DictionaryEntry myDE in myNameHashtable )
Debug.WriteLine("\t"+myDE.Key.ToString()+":"+myDE.Value.ToString());
}
// Displays values of an array.
public static void DisplayArrayValues ( Array array )
{
for ( int i = 0; i < array.Length; i++ )
{
Debug.Write(array.GetValue(i)+" ");
}
}
}
/*
The output of the sample.
Initial contents of the NameHashtable(key:value):
Coconut:0.99
Orange:0.89
Strawberry:1.29
Lemon:1.49
Banana:1.29
Apple:1.49
Contains "Coconut":True
Contents of the NameHashtable after removing the "Coconut" key element(key:value):
Orange:0.89
Strawberry:1.29
Lemon:1.49
Banana:1.29
Apple:1.49
Initial contents the array of string:abc def ghi jkl mno pqr stu
Contents of the array after copying the keys of the NameHashtable in it:Orange Strawberry Lemon Banana Apple pqr stu
Contents of the NameHashtable after setting the "Apple" key element to "test":
Orange:0.89
Strawberry:1.29
Lemon:1.49
Banana:1.29
Apple:test
*/
Namespace System.Collections.Specialized Flash Library System.scl Flash Library Version 2.0.0.1308 Silverlight Library System.Windows.Forms.dll
| © 2003-2007 NETiKA Technologies. All rights reserved. |