103

I have made a dictionary which contains two values: a DateTime and a string.

Now I want to print everything from the dictionary to a Textbox. Does anybody know how to do this?

I have used this code to print the dictionary to the console:

private void button1_Click(object sender, EventArgs e) { Dictionary<DateTime, string> dictionary = new Dictionary<DateTime, string>(); dictionary.Add(monthCalendar1.SelectionStart, textBox1.Text); foreach (KeyValuePair<DateTime, string> kvp in dictionary) { //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value); Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } } 
3
  • 2
    what about the line you have commented out? Commented Feb 17, 2015 at 0:15
  • 1
    That gives the following error: Only assignement, call, increment, await and new object expressions can be used as a statement. Commented Feb 17, 2015 at 0:18
  • 8
    I think you're just missing a string.Format in the commented out line. Commented Feb 17, 2015 at 0:18

5 Answers 5

136

Just to close this

foreach (KeyValuePair<DateTime, string> kvp in dictionary) { //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value); Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } 

Changes to this

foreach (KeyValuePair<DateTime, string> kvp in dictionary) { //textBox3.Text += ("Key = {0}, Value = {1}", kvp.Key, kvp.Value); textBox3.Text += string.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value); } 
Sign up to request clarification or add additional context in comments.

Comments

98

Cleaner way using LINQ:

var lines = dictionary.Select(kvp => kvp.Key + ": " + kvp.Value.ToString()); textBox3.Text = string.Join(Environment.NewLine, lines); 

kvp is short for "key-value pair".

1 Comment

with $ - string interpolation kvp => $"{kvp.Key}:{kvp.Value}"
28

There's more than one way to stringify a dictionary; here's my solution:

  1. Use Select() to convert the key-value pair to a string;
  2. Convert to a list of strings;
  3. Write out to the console using ForEach().
dict.Select(i => $"{i.Key}: {i.Value}").ToList().ForEach(Console.WriteLine); 

Comments

23

There are so many ways to do this, here is some more:

string.Join(Environment.NewLine, dictionary.Select(a => $"{a.Key}: {a.Value}")) 
dictionary.Select(a => $"{a.Key}: {a.Value}{Environment.NewLine}")).Aggregate((a,b)=>a+b) 
new String(dictionary.SelectMany(a => $"{a.Key}: {a.Value} {Environment.NewLine}").ToArray()) 

Additionally, you can then use one of these and encapsulate it in an extension method:

public static class DictionaryExtensions { public static string ToReadable<T,V>(this Dictionary<T, V> d){ return string.Join(Environment.NewLine, d.Select(a => $"{a.Key}: {a.Value}")); } } 

And use it like this: yourDictionary.ToReadable().

2 Comments

Isnt it kind of a shame that you cant print lists/collections just like that?
This is beautiful, thank you: string.Join(Environment.NewLine, dictionary.Select(a => $"{a.Key}: {a.Value}"))
6

My goto is

Console.WriteLine( Serialize(dictionary.ToList() ) ); 

Make sure you include the package using static System.Text.Json.JsonSerializer;

1 Comment

I also needed to add using System.Linq for the .ToList()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.