Is there anyway to handy convert a dictionary to String?

后端 未结 12 1227
死守一世寂寞
死守一世寂寞 2020-12-13 23:50

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?

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 23:57

    No handy way. You'll have to roll your own.

    public static string ToPrettyString(this IDictionary dict)
    {
        var str = new StringBuilder();
        str.Append("{");
        foreach (var pair in dict)
        {
            str.Append(String.Format(" {0}={1} ", pair.Key, pair.Value));
        }
        str.Append("}");
        return str.ToString();
    }
    

提交回复
热议问题