converting hashtable to arraylist

A

Alexander Widera

hello,

how can I convert a hashtable to an arraylist or array? I want to sort the
hashtable... but this isn't posible, so i have to convert it to an
hashtable.

thank you,
alex
 
G

Guest

I see here one big problem:
1. HashTable is indexed with string key but ArrayList and Array have integer
indexer.
2. If you don't care about key in HashTable try this method:
HashTable.CopyTo(Array array, int arrayIndex)

The elements are copied to the Array in the same order in which the
enumerator iterates through the Hashtable.

To copy only the keys in the Hashtable, use Hashtable.Keys.CopyTo.

To copy only the values in the Hashtable, use Hashtable.Values.CopyTo.



http://msdn.microsoft.com/library/d...frlrfsystemcollectionshashtableclasstopic.asp
 
Joined
Jan 5, 2009
Messages
2
Reaction score
0
Hi,
I was stuck with the similar problem but I wanted to convert Hashtable to List<> so that I can save both key and corresponding value.Is this possible?


Regards,
Rashmi
greatdevelopers.com
 
Joined
Jan 27, 2010
Messages
27
Reaction score
0
How To Iterate the Hashtable in C#

If we write in the same way as above for the hashtable then we would not get the desired values.

Hashtable ss = new Hashtable();
ss["key1"] ="india";
ss["key2"] = "bharat";

foreach (object gg in ss)
{
Console.WriteLine("Key value is " + gg);
Console.Read();
}

Here we get System.Collections.DictionaryEntry and System.Collections.DictionaryEntry as output instead of the value pairs stored in the hashtable.

In this case we can help of DictinaryEntry object for iterating the hashtable.

foreach(DictionaryEntry gg in ss)
{
Console.WriteLine("Key and value are " + gg.Key + " " + gg.Value);
Console.Read();
}

A Dictionary Entry is simply a container containing the Key and Value .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top