If you just want to serialize for debugging purposes, the shorter way is to use String.Join
:
var asString = string.Join(Environment.NewLine, dictionary);
This works because IDictionary<TKey, TValue>
implements IEnumerable<KeyValuePair<TKey, TValue>>
.
Example
Console.WriteLine(string.Join(Environment.NewLine, new Dictionary<string, string> {
{"key1", "value1"},
{"key2", "value2"},
{"key3", "value3"},
}));
/*
[key1, value1]
[key2, value2]
[key3, value3]
*/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…