The best way to do it is like this:
txtCarros.Text = string.Join("\t", novoCarro.mostraModelo(), novoCarro.mostraPlaca(), novoCarro.mostraKm(), "\n");
Concatenate string manually generates several allocations and this in addition to being slow creates pressure on the garbage collector. This case doesn’t do a lot of damage, but if the number of items increases, it gets exponentially worse.
I put the tabs that is the question asks, and the line break only at the end.
In a simplified form:
public class Program { public static void Main() => System.Console.WriteLine(string.Join("\t", "modelo", "placa", "km", "\n")); }
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
Documentation of Join()
.
But if you are doing everything in the same expression it is possible to simplify a little the syntax that the compiler will transform into Concat()
for you, and there fits even better:
txtCarros.Text = novoCarro.mostraModelo() + "\t" + novoCarro.mostraPlaca() + "\t" novoCarro.mostraKm() + "\n";
For a few items it is not worth using the StringBuilder
, especially when you don’t know the final size.
Another alternative would be:
txtCarros.Text = $"{novoCarro.mostraModelo()}\t{novoCarro.mostraPlaca()}\t {novoCarro.mostraKm()}\n";
Wouldn’t r n? or n
– Antonio S. Junior
i tested with only /n and the result was really, but just in case I’ll edit the topic
– user98341
test with the backslash? n?
– Antonio S. Junior
another way would be txtCarros.Text += Environment.Newline + newCarro.shows()
– Antonio S. Junior
My, my, my, that’s right :)
– user98341
Blz. I will add as an answer only to help anyone with the same question.
– Antonio S. Junior