1. Home
  2. Computing & Technology
  3. Delphi Programming
Delphi for .NET FCL Examples Reference
HashTable namespace
System.Collections
Declaration
Hashtable = class(System.Object, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable);
Description
Represents a collection of key-and-value pairs that are organized based on the hash code of the key.
Example
program System_Collections_HashTable;
{$APPTYPE CONSOLE}
{$AUTOBOX ON}

uses
  System.Collections;

type
  SamplesHashtable = class
  public
    class procedure Main;
  private
    class procedure PrintKeysAndValues(myList: Hashtable);
  end;

{ SamplesHashtable }

class procedure SamplesHashtable.Main;
var
  myHT: Hashtable;
begin
  // Creates and initializes a new Hashtable
  myHT := Hashtable.Create;
  myHT.Add('First', 'Hello');
  myHT.Add('Second', 'World');
  myHT.Add('Third', '!');

  // Displays the properties and values of the Hashtable
  Console.WriteLine('myHT');
  Console.WriteLine('  Count:    {0}', myHT.Count);
  Console.WriteLine('  Keys and Values:');
  
  PrintKeysAndValues(myHT);
end;

class procedure SamplesHashtable.PrintKeysAndValues
                (myList: Hashtable);
var
  myEnumerator: IDictionaryEnumerator;
begin
  myEnumerator := myList.GetEnumerator;
  Console.WriteLine(''#9'-KEY-'#9'-VALUE-');
  while myEnumerator.MoveNext do
  begin
    Console.WriteLine(''#9'{0}:'#9'{1}', 
                      myEnumerator.Key, 
                      myEnumerator.Value);
  end;
  Console.WriteLine;
end;

//test program
begin
  SamplesHashtable.Main;
end.
Example output
myHT
  Count:    3
  Keys and Values:
    -KEY-    -VALUE-
    Third:    !
    Second:    World
    First:    Hello

Looking for Delphi for .NET examples? Look no further, Delphi for .NET FCL Examples Reference is what you are looking for!

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.