Search code examples
c#asp.netstringhashtable

Retrieve all keys in a hash table into a string


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


Solution

  • 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.