Printing Array of Strings in a Single MessageBox

2

I have a variable that is an array of string s and I want to print its values, but these values and consequently the quantity of them is only defined after the system works.

How can I print in a MessageBox , for example, all values of all positions in only MessageBox instead of making a for and print one by one?

    
asked by anonymous 24.08.2015 / 15:06

4 answers

3

Simply use String.Join

string.Join(", ", valores);

Fiddle: link

    
24.08.2015 / 16:37
3

Another option would be as follows

var dataTransacao = new string[] {"01/01/2015", "02/02/2015", "03/03/2015" };
MessageBox.Show(String.Format("Datas de transação: {0}", String.Join(", ", dataTransacao)), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    
24.08.2015 / 16:29
3

You can use string.Join()

In the example below the variable strDados contains all comma-separated array items.

string[] dados = { "um", "dois", "três" };
string strDados = string.Join(", ", dados);

MessageBox.Show(strDados);

The MessageBox will show:

  

one, two, three

    
24.08.2015 / 16:37
1

You can make this impression by using the Aggregate . For example:

MessageBox.Show(dataTransacao.Aggregate( (acumulador, posicao) => acumulador + ", " + posicao, "Título"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

If you declare dataTransacao like this:

var dataTransacao = new string[] {"01/01/2015", "02/02/2015", "03/03/2015" };

It will exit in MessageBox like this:

01/01/2015, 02/02/2015, 03/03/2015
    
24.08.2015 / 16:01