Convert Dictionary To String In C#

In this blog, you will learn how to convert a dictionary to string in C#.

To achieve this, we can use the following namespace: "System.Collections.Generic". Here is a custom code snippet, where the single parameter is the Dictionary<string, string> parameter. You can change key and value type of a dictionary as per need.
  1. public string DictionaryToString(Dictionary < stringstring > dictionary) {  
  2.  string dictionaryString = "{";  
  3.  foreach(KeyValuePair < stringstring > keyValues in dictionary) {  
  4.   dictionaryString += keyValues.Key + " : " + keyValues.Value + ", ";  
  5.  }  
  6.  return dictionaryString.TrimEnd(','' ') + "}";  
  7. }