'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 1:[1]

You can just use string.Join to make them into one string. Don't, use \n, it's better to use Environment.NewLine

string msg = string.Join(Environment.NewLine, array);

Solution 2:[2]

I would see two common ways to do this.

        // Short and right on target
        string[] array = {"item1", "item2", "item3"};
        string output = string.Join("\n", array);
        MessageBox.Show(output);


        // For more extensibility.. 
        string output = string.Empty;
        string[] array = { "item1", "item2", "item3" };
        foreach (var item in array) {
            output += item + "\n"; 
        }

        MessageBox.Show(output);

Solution 3:[3]

try Using this..

using System.Threading;



string[] array = {"item1", "item2", "item3"};


        foreach (var item in array)
        {

            new Thread(() =>
            {
                MessageBox.Show(item);
            }).Start();


        }

Solution 4:[4]

Put the message box out of the loop like this

string[] array = {"item1", "item2", "item3"};

string message5 = "";

foreach (var item in rates)
{
    message5 += item + "\n" ;
  
}
MessageBox.Show(message5);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Oskar Kjellin
Solution 2 Independent
Solution 3 kawafan
Solution 4 Syscall