Search code examples
c#dictionarytostring

Is there anyway to handy convert a dictionary to String?


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?


Solution

  • 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()) + "}";
    }