views:

75

answers:

2

Hi Guys,
The coding language is C#3.0
What is the most optimum method to retrieve all hashtable keys into string separated by a delimiter ","
Is the for loop or the foreach loop the only option?

Update: the keys are already strings

Regards,
naveenj

+3  A: 

Do you really mean a non-generic Hashtable? You could use LINQ, assuming that's available to you:

string keys = string.Join(",", table.Keys.Cast<object>()
                                         .Select(x => x.ToString())
                                         .ToArray());

There may be faster ways, but that's the way I'd go for the sake of readability. Only micro-optimize when you've proved it's a bottleneck.

Jon Skeet
thanks jon.. even so it will type cast even if its an object.. just great...
naveen
A: 

You can also use the IDictionaryEnumerator:

IDictionaryEnumerator enum = table.GetEnumerator();
while (enum.MoveNext())
{
   text += enum.Key + ", ";
   text += enum.Value + "\n";
}