0

the best result I've found so far is this

List<int> pL = new List<int> { 1, 3, 5, 10 };
Console.Write("[");

for (int i = 0; i < pL.Count-1; i++)
    Console.Write("{0}, ", pL[i]);

Console.Write(pL[pL.Count-1] + "]");

but is there a better way, less bulky.

Filburt
  • 17,626
  • 12
  • 64
  • 115
Jack
  • 3
  • 1

2 Answers2

1

String.Join can take any enumerable (which List<string> is), so to concatenate all items in a list with a comma you can use

var result = String.Join(", ", pL);

You can also use string interpolation, so

Console.WriteLine($"[{string.Join(", ", pL)}]");
Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

you can join the list and concatenate the pre and postfix

List<int> pL = new List<int> { 1, 3, 5, 10 };
var result = "[" + String.Join(", ", pL) + "]";
Console.WriteLine(result);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97