Is there anyway to handy convert a dictionary to String?

user705414 picture user705414 · May 5, 2011 · Viewed 76.2k times · Source

I found the default implemtation of ToString in the dictionary is not what I want. I would like to have {key=value, ***}.

Any handy way to get it?

Answer

Tim Rogers picture Tim Rogers · May 5, 2011

Try this extension method:

public static string ToDebugString<TKey, TValue> (this IDictionary<TKey, TValue> dictionary)
{
    return "{" + string.Join(",", dictionary.Select(kv => kv.Key + "=" + kv.Value).ToArray()) + "}";
}