Is there anyway to handy convert a dictionary to String?

后端 未结 12 1256
死守一世寂寞
死守一世寂寞 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-14 00:14

    Maybe:

    string.Join
    (
        ",",
        someDictionary.Select(pair => string.Format("{0}={1}", pair.Key.ToString(), pair.Value.ToString())).ToArray()
    );
    

    First you iterate each key-value pair and format it as you'd like to see as string, and later convert to array and join into a single string.

提交回复
热议问题