Basically, I have a list with multiple items in it and I want a single message box to display them all.
The closest I have got is a message box for each item (using foreach
).
What I want is the equivalent of:
MessageBox.Show ("List contains:"+
Foreach (string str in list)
{ str + Environment.Newline + }
)
But obviously this won't work! What is the correct way of doing this?
You can join everything into a single string with string.Join
:
var message = string.Join(Environment.NewLine, list);
MessageBox.Show(message);
However, if you don't have access to .NET 4, you don't have that overload that takes an IEnumerable<string>
. You will have to fallback on the one that takes an array:
var message = string.Join(Environment.NewLine, list.ToArray());
MessageBox.Show(message);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With