Search code examples
c#arraysmessagebox

C# MessageBox For All Items In Array


I am trying to iterate through an array of strings and present all of them in a single messagebox. The code I have at the minute is this:

string[] array = {"item1", "item2", "item3"};
foreach(item in array)
{
   MessageBox.Show(item);
}

This obviously brings up a messagebox for each item, is there any way I can show them all at once in a messagebox outside the loop? I will be using \n to separate the items if this is possible, thanks.


Solution

  • You can combine the individual strings from the array into a single string (such as with the string.Join method) and then display the concatenated string:

    string toDisplay = string.Join(Environment.NewLine, array); 
    MessageBox.Show(toDisplay);