Paul Stanley
Paul Stanley

Reputation: 2161

String.Format Format a array

Is it possible to format a array of values when you don't know in advance the number of elements in the array. I have tried this:

 static void Main(string[] args)
    {
        object[] x = { 1, 2, 3 };
        Console.WriteLine(string.Format("{0}", x));
        Console.ReadKey();
    }

This produces "1".

I am trying to output 1,2,3 or "1","2","3"

Upvotes: 2

Views: 2367

Answers (1)

pwas
pwas

Reputation: 3373

Use string.Join:

var result = string.Join(",", x); // 1,2,3

or:

var result = string.Join(",", x.Select(n => "\"" + n + "\"")); // "1","2","3"

Reffer MSDN

Upvotes: 8

Related Questions