2

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"

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.