I believe you see the string concatenation in books for beginner because it is simple. String formatting needs to be explained before it can be used in such simple example. String concatenation is much simple and may already have been taught at that point in the book (even if not, it is simple enough to learn by example).
I can see how C programmers might be confused by the format syntax when encountering it for the first time. When attempting to extend it to two variables they might think it needs to be written as so:
int numA = 3;
int numB = 5;
Console.WriteLine("numA is {0} and numB is {0}", numA, numB);
Thinking it would be similar to C's printf
where {0}
is equivilant to %d
:
printf("numA is %d and numB is %d", numA, numB);
They would of course be surprised to have varA
be printed twice. Or they may be frustrated of not knowing the equivalent of %s
when trying to Console.WriteLine
a string. String concatenation, on the other hand, has fewer pitfalls and can be more easily extended by beginners. Of course it is more cluttered but also more powerful, power that may be confusing in a book introducing someone to C# since string formatting syntax can grow quite complex:
Console.WriteLine("numA is {0,17:$00.00####}", numA);
The above example also illustrates the difference between concatenation and string formatting. They are different, but for simple usages like the one you made, it makes little difference.