Just started working with C# for the first time, and while looking through the tutorial, I found nothing on the difference between the Concatenation (console.writeline("Hello" + user)
where user is a string variable) and the place holder (console.writeline("Hello {0}" , user
) where user is a string variable) methods for output. Is there a difference or is it simply which way you find easier

- 25,351
- 17
- 103
- 158

- 11
- 2
-
1`{0}` uses for formatting, also you may want to look at `Operator Overloading` for example `string strHold += strHold + some string` – MethodMan Feb 07 '13 at 19:06
-
Consider String.Format() for the latter way of using it. – bash.d Feb 07 '13 at 19:07
-
Format strings are easier to read and modify, in my opinion. I hate looking at concatenated strings that have more quotation marks and plus signs than actual characters. – JosephHirn Feb 07 '13 at 19:22
3 Answers
Its not really specific to C#, lots of languages support both styles. The latter form is usually thought of as 'safer', but I can't quote any specific reason why. It is useful if the item needs to appear in more than 1 place, or if you want to save the format string as a constant. Take a look at this thread for more info: When is it better to use String.Format vs string concatenation?.

- 1
- 1

- 174
- 5
Using string formatters, as opposed to string concatenation, is almost entirely about readability. What they actually do, and even how they perform, is close enough to the same.
For such a simple case both look all right, but when you have a complex string with lots of values mixed in format strings can end up looking a lot nicer:
Here's a better example:
string output = "Hello " + username + ". I have spent " + executionTime + " seconds trying to figure out that the answer to life is: " + output;
vs
string output = string.Format("Hello {0}. I have spent {1} seconds trying to figure out that the answer to life is: {2}"
, username, executionTime, output);

- 202,030
- 26
- 332
- 449
As Matt said Place holding is considered as the safer approach then the simple concatenation, but I am not sure for that reasons(I need to explore on it). But yes one thing is sure that Place Holding is a bit costly operation then Concatenation in terms of Performance. Check this Blog entry "Formatting Strings" by Jon Skeet.
Although performance will be effected significantly only if you are using Place Holders for like thousands times or so.

- 1,336
- 10
- 13