Howdy,
Hastable does not retain the order because items are oragnised by the key
hash code, allowing access in constant number of operations O(n). You would
have to create an additional list containing keys:
Hashtable hashtable =
new Hashtable();
List<string> history =
new List<string>();
for (int i = 0; i < 10; i++)
{
string key = Guid.NewGuid().ToString();
hashtable.Add(key, Guid.NewGuid());
history.Add(key);
}
// show how the items are organised in hashtable
foreach (DictionaryEntry pair in hashtable)
{
System.Diagnostics.Debug.WriteLine(
pair.Key.ToString() + "=" +
pair.Value.ToString());
}
System.Diagnostics.Debug.WriteLine(String.Empty);
// show items in order they were added
foreach (string key in history)
{
System.Diagnostics.Debug.WriteLine(
key + "=" +
hashtable[key].ToString());
}
Hope this helps
Milosz