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
Just for fun and in case you need to do something like that with non-string collections one time - a LINQ version using Aggregate
, which is the closest to your example syntax. Don't use it here, do indeed use String.Join
in this case, but keep in mind that you have something in LINQ that can do what you need.
MessageBox.Show("List contains: " +
list.Aggregate((str,val) => str + Environment.NewLine + val);
EDIT: also, like Martinho Fernandes pointed out, it's better to use the StringBuilder
class in cases like that, so:
MessageBox.Show("List contains: " + list.Aggregate(new StringBuilder(),
(sb,val) => sb.AppendLine(val),
sb => sb.ToString()));