Convert a Dictionary to a String in C#
This post will discuss how to convert a Dictionary<TKey,TValue>
to a string in C#.
1. Using String.Join()
method
The String.Join() method from the System.Linq
namespace is commonly used to concatenate elements of the specified sequence using the specified separator. You can use the Select() method as a helper while joining each key-value pair in a dictionary with a delimiter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static string GetString<K,V>(this IDictionary<K,V> dict) { var items = dict.Select(kvp => kvp.ToString()); return string.Join(", ", items); } } public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; string str = dict.GetString(); Console.WriteLine(str); // [A, 1], [B, 2], [C, 3], [D, 4], [E, 5] } } |
Instead of calling the ToString()
method, you can format the key-value pair as desired. The following solution demonstrates this by using the LINQ Query syntax to project over the dictionary mappings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static string GetString<K,V>(this IDictionary<K,V> dict) { var items = from kvp in dict select kvp.Key + ":" + kvp.Value; return "{" + string.Join(", ", items) + "}"; } } public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; string str = dict.GetString(); Console.WriteLine(str); // {A:1, B:2, C:3, D:4, E:5} } } |
2. Using Enumerable.Aggregate()
method
Alternatively, you can use the LINQ’s Aggregate() method, which applies an accumulator function on each element of a sequence. It can be used as follows to stringify a dictionary.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.Linq; using System.Collections.Generic; public static class Extensions { public static string GetString<K,V>(this IDictionary<K,V> dict) { return dict.Select(kvp => kvp.ToString()) .Aggregate((x, y) => x + ", " + y); } } public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; string str = dict.GetString(); Console.WriteLine(str); // [A, 1], [B, 2], [C, 3], [D, 4], [E, 5] } } |
3. Using StringBuilder.Append()
method
Finally, you can write your custom logic to stringify a dictionary. The idea is to iterate over the dictionary using a foreach loop and append all the dictionary entries to a StringBuilder
using its Append()
method. Then, invoke the ToString()
method to get the string representation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Linq; using System.Text; using System.Collections.Generic; public static class Extensions { public static string GetString<K,V>(this IDictionary<K,V> dict) { var sb = new StringBuilder(); foreach (var pair in dict) { sb.Append(String.Format("[{0}:{1}] ", pair.Key, pair.Value)); } return sb.ToString(); } } public class Example { public static void Main() { Dictionary<string, int> dict = new Dictionary<string, int>() { {"A", 1}, {"B", 2}, {"C", 3}, {"D", 4}, {"E", 5} }; string str = dict.GetString(); Console.WriteLine(str); // [A:1] [B:2] [C:3] [D:4] [E:5] } } |
That’s all about converting a Dictionary<TKey,TValue>
to a string in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)