System.Collections.Hashtable
7 gennaio 2010
Nessun commento
Rappresenta un insieme di coppie chiave/valore organizzate in base al codice hash della chiave.
using System;
using System.Collections;
class Example
{
public static void Main()
{
Hashtable htTest = new Hashtable();
htTest.Add("txt", "notepad.exe");
htTest.Add("bmp", "paint.exe");
htTest.Add("dib", "paint.exe");
htTest.Add("rtf", "wordpad.exe");
try
{
htTest.Add("txt", "winword.exe");
}
catch
{
Console.WriteLine("Un elemento con 'Key' = 'txt' esiste gia.");
}
Console.WriteLine("Per la 'key' = 'rtf', 'Value' = {0}.", htTest["rtf"]);
htTest["rtf"] = "winword.exe";
Console.WriteLine("Per la 'key' = 'rtf', 'Value' = {0}.", htTest["rtf"]);
htTest["doc"] = "winword.exe";
if (!htTest.ContainsKey("ht"))
{
htTest.Add("ht", "hypertrm.exe");
Console.WriteLine("Valore aggiunto con 'key' = 'ht', 'Value' = {0}", htTest["ht"]);
}
Console.WriteLine();
foreach( DictionaryEntry de in htTest )
{
Console.WriteLine("'Key' = {0}, 'Value' = {1}", de.Key, de.Value);
}
ICollection valueColl = htTest.Values;
Console.WriteLine();
foreach( string s in valueColl )
{
Console.WriteLine("'Value' = {0}", s);
}
ICollection keyColl = htTest.Keys;
Console.WriteLine();
foreach( string s in keyColl )
{
Console.WriteLine("'Key' = {0}", s);
}
Console.WriteLine("\nRimuovo elemento con chiave = 'doc'");
htTest.Remove("doc");
if (!htTest.ContainsKey("doc"))
{
Console.WriteLine("L'elemento con chiave = 'doc' non esiste.");
}
}
}