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<stringhist ory =
new List<string>();
for (int i = 0; i < 10; i++)
{
string key = Guid.NewGuid(). ToString();
hashtable.Add(k ey, Guid.NewGuid()) ;
history.Add(key );
}
// show how the items are organised in hashtable
foreach (DictionaryEntr y pair in hashtable)
{
System.Diagnost ics.Debug.Write Line(
pair.Key.ToStri ng() + "=" +
pair.Value.ToSt ring());
}
System.Diagnost ics.Debug.Write Line(String.Emp ty);
// show items in order they were added
foreach (string key in history)
{
System.Diagnost ics.Debug.Write Line(
key + "=" +
hashtable[key].ToString());
}
Hope this helps
Milosz
--
Milosz
"rk****@gmail.c om" wrote:
I am using Hashtable to keep Key-Value pair of elements.
When I add the items to the Hashtable it does not retain the order in
which I have added the key-value pair.
Is there anyway to retain the order?