.NET: efficient way to produce a string from a Dictionary<K,V> ?

Posted by Cheeso on Stack Overflow See other posts from Stack Overflow or by Cheeso
Published on 2010-05-12T14:08:12Z Indexed on 2010/05/12 14:14 UTC
Read the original article Hit count: 176

Suppose I have a Dictionary<String,String>, and I want to produce a string representation of it. The "stone tools" way of doing it would be:

private static string DictionaryToString(Dictionary<String,String> hash)
{
    var list = new List<String> ();
    foreach (var kvp in hash)
    {
        list.Add(kvp.Key + ":" + kvp.Value);
    }
    var result = String.Join(", ", list.ToArray());
    return result;
}

Is there an efficient way to do this in C# using existing extension methods?

I know about the ConvertAll() and ForEach() methods on List, that can be used to eliminate foreach loops. Is there a similar method I can use on Dictionary to iterate through the items and accomplish what I want?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about dictionary