I want to print an array
via Console.writeline
.
I'm lazy and want to do it in one line, avoiding iterating through all the array
.
Here is my code:
var costumers = new Costumers[10];
// Array initialization...
Console.WriteLine("Initial array: '{0}'",string.Join(Environment.NewLine,costumers.ToList()) );
Array.Sort(costumers);
Is it OK to use String.Join
for this purpose or it decreases performance dramatically?
Is there an elegant way to do it?
Thanks, Paul
There is absolutely nothing wrong with using String.Join
: the method is so straightforward that there is hardly anything that could go wrong implementing it. All you need is a StringBuilder
, a single loop, and a bool
flag to skip pre-pending the separator the first time around.
You could slightly improve your code by removing ToList()
from the call, because Join
accepts IEnumerable<T>
:
Console.WriteLine("Initial array: '{0}'", string.Join<Costumer>(Environment.NewLine, costumers) );