All you have done is considered one of the the most simplistic forms of a
'formatting' overload of the Console.WriteLine method.
Consider:
Console.WriteLine("Employee Name = {0,-30} {1,30}", employee.FirstName,
employee.LastName);
which gives specific alignment the output.
Consider also the myriad of formatting options you have for numeric values.
Try simulating those with simple string concatenation.
"Author" <gn********@gmail.comwrote in message
news:8f**********************************@34g2000h sh.googlegroups.com...
On Jun 23, 2:11 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Author <gnewsgr...@gmail.comwrote:
I have always been wondering if there is any significant different
between doing
System.Console.WriteLine("Employee Name = " + employee.FirstName + " "
+ employee.LastName);
and
System.Console.WriteLine("Employee Name = {0} {1}",
employee.FirstName, employee.LastName);
Is it a matter of preference or is there a performance issue with the
first version rising from creating immutable strings through
concatenation?
The first version is likely to be faster than the second. The first one
just calls string.Concat(string, string, string, string). The second
needs to parse the format string, work out the placeholders and then do
the concatenation.
However, all of this is likely to be dwarfed by the cost of actually
writing to the console. As always, write for readability first and if
you find there's a performance problem, measure, measure, measure!
--
Jon Skeet - <sk...@pobox.com>
Web site:http://www.pobox.com/~skeet
Blog:http://www.msmvps.com/jon_skeet
C# in Depth:http://csharpindepth.com
OK, thank you very much. Just curious, why do they introduce the
second version (the one using String.Format), which is sorta like the
tradtional C syntax of printf?