4

I've often been puzzled whether there should be any preference over any of these two given styles for printing out strings that require minor concat.

string temp = "test";

        Console.WriteLine("this is a " + temp);
        Console.WriteLine("this is a {0}", temp);

Is there any benefit/detriment, using one over the other or is it simply down to preference?

Thanks.

Darren Young
  • 10,972
  • 36
  • 91
  • 150
  • 2
    Have you checked out the IL for it? – Tim Barrass Dec 17 '10 at 11:03
  • 2
    Have a look at this question: http://stackoverflow.com/q/16432/261677 My preference is string.Format mostly for readability but there's a few timings done in the answers – Jonathon Bolster Dec 17 '10 at 11:05
  • Thanks. TBH I have never looked at any IL....i'm still very much a learner in C#. Will put that on my long list of things to do :) – Darren Young Dec 17 '10 at 11:07
  • just posted an answer with an example, but you'd have to find some way of instrumenting it to find out if there were some real perf hit. Check out ildasm :) – Tim Barrass Dec 17 '10 at 11:11

6 Answers6

4

As you said, any performance issues on that scale are minuscule, so use your preference.

I'll use the first method occasionally in a throw-away prototype or a proof-of-concept application, but I'll use the second method exclusively in anything that will potentially reach production.

My logic is that the first format is easier, and more natural to write quickly, but the second one is more suitable for reading and maintaining, e.g. how will they change if you need to add a . to the end of the sentence?

Console.WriteLine("this is a " + temp + "." );
vs
Console.WriteLine("this is a {0}.", temp);
SWeko
  • 30,434
  • 10
  • 71
  • 106
4

EDITED: My original answer just pointed at the IL for the two techniques; my intent was to suggest that Darren take those as a starting point. Ani (comments) suggested that wasn't enough for a clear answer. I decided to take a look as well, so have posted here an outline of the process I was suggesting to Darren which hopefully shows the process to go through, and reveals how others might be able to immediately say "o, x uses a string::format".

So: naively I'd expect the first to be more efficient as all the CLR needs to to is combine two strings, whereas the second uses a generalized method that takes an object and therefore needs to convert that object to a string. Even if that conversion is trivial it still needs to go through some plumbing to get to the call before finally finding that the string doesn't need converting. I took a look at the IL using ildasm to see what was happening -- equally we could use Reflector which would potentially be more readable.

For what it's worth -- I think I'd be using StringBuilder.Append from the start, in any case.

In the first (+) instance we have a call to String::Concat(string, string) (which does pretty much what you'd expect if you look at the IL), followed by a Console.WriteLine(string).

  IL_000d:  call       string [mscorlib]System.String::Concat(string,
                                                              string)
  IL_0012:  call       void [mscorlib]System.Console::WriteLine(string)

Console.WriteLine(string) effectively just calls TextWriter::WriteLine(string). So much for the first method.

The second method calls Console.WriteLine(string, object):

  IL_000d:  call       void [mscorlib]System.Console::WriteLine(string,
                                                                object)

If we disassemble Console::WriteLine (in mscorlib) we see it calls TextWriter::WriteLine(string, object):

.method public hidebysig static void  WriteLine(string format,
                                                object arg0) cil managed
...
  IL_0007:  callvirt   instance void System.IO.TextWriter::WriteLine(string,
                                                                     object)

which, disassembled, calls String::Format(...):

.method public hidebysig newslot virtual 
        instance void  WriteLine(string format,
                                 object arg0) cil managed
...
  IL_0014:  call       string System.String::Format(class System.IFormatProvider,
                                                    string,

In this case, String::Format actually creates a StringBuilder with the initial string:

.method public hidebysig static string  Format(class System.IFormatProvider provider,
                                               string format,
...
  IL_0027:  newobj     instance void System.Text.StringBuilder::.ctor(int32)

then has to call StringBuilder::AppendFormat with the object

  IL_0031:  callvirt   instance class System.Text.StringBuilder System.Text.StringBuilder::AppendFormat(class System.IFormatProvider,
                                                                                                        string,
                                                                                                        object[])

in order to fill it out. StringBuilder::AppendFormat then has to convert the object so that it can append it as a string before finally calling TextWriter::WriteLine(string).

Summarising that lot, both eventually call TextWriter::WriteLine(string), so they differ in that the first calls

String::Concat(string, string) 

and the second calls

Console::WriteLine(string, object), 
TextWriter::WriteLine(string, object)
String::Format(...).  
String::StringBuilder().ctor
String::StringBuilder().AppendFormat(...)

(and plumbing) to basically do the same job.

There's a surprising amount going on under the hood in the second. StringBuilder::ApendFormat is easily the most complicated part of all, and in fact my IL isn't good enough to work out whether it has early escapes if e.g. it finds out that the object passed is a string ... but the fact that it has to consider it means it has to do some extra work.

So, there are underlying differences between the two. The first looks more efficient if you know you have two strings to combine, which may be a benefit. It'd be interesting to take a look at how the first compares with using a StringBuilder from the outset.

Comments and corrections welcome .. and hope it helps clarify my answer.

Tim Barrass
  • 4,813
  • 2
  • 29
  • 55
  • Thanks. I didn't realise all this was being performed in the background. I'll certainly spend some time getting to grips with this, seems very useful. – Darren Young Dec 17 '10 at 11:14
  • Whoever downvoted -- fair enough, but could you exlpain why, then we can all learn from it. – Tim Barrass Dec 17 '10 at 11:22
  • If that's actually @Tim -- I disagree, it's quite an open question about the "benefit/detriment, using one over the other" (quote). I interpreted that to mean "what's actually going on here?" – Tim Barrass Dec 17 '10 at 11:25
  • I don't see the relevance of this answer to be honest; it's almost a *restatement* of the question in a different language. Perhaps the only marginally useful information in this answer is that `+` involves a call to `string.Concat`, which I think most people are aware of already. Now if you'd posted the inner workings of `string.Concat` vs `Console.WriteLine(string, obj)` (which involves a call to `string.Format`), that would have meant something, but.. – Ani Dec 17 '10 at 11:30
  • What do you mean by "they'll both have to parse to string at some point"? – Ani Dec 17 '10 at 11:32
  • (My original comment that Tim responded to was incorrectly addressed to the OP, it read: "I think you're focusing on the wrong thing, this is a straight "Concatenation vs format-string" question.") – Ani Dec 17 '10 at 11:35
  • @Ani looking over the answer they're mostly restatements of the question, with a minor tweak. Again, I think this is more an starting point exploration of what's going on -- Darren hadn't thought of looking at what's going on underneath more deeply. Clearly understanding what concat adn writeline do under the hood is the next step, and one he can take himself as well -- it sounds like another new SO question to me. – Tim Barrass Dec 17 '10 at 11:35
  • Parsing to string was a reference to the writeline taking an object, not a string, and was incompletely considered. I'm answering your comments, not editing my answer :) – Tim Barrass Dec 17 '10 at 11:37
  • 1
    Ok, let me try to put it this way. Most of the other answers talk about maintainable code, readability etc. etc. You decided to approach this purely from a perf perspective instead. That's fine. Unfortunately, the *long* IL you've posted reveals *nothing* about the respective perf characteristics of each option - it just *restates* the question in a different language. That's why I didn't find it useful. – Ani Dec 17 '10 at 11:42
  • That's fine, and I see your point entirely. You're missing my point, which was to show Darren a way of investigating what's going on in more detail. At no point do I even claim that there is a performance hit, just that there are some ways in which he can investigate more deeply. Ultimately he asked the question, and he found the answer useful, and he's entirely within his rights to reconsider. – Tim Barrass Dec 17 '10 at 11:50
  • "Ultimately he asked the question, and he found the answer useful" - I agree. I only criticised to help, because *I* didn't find it useful. Cheers. – Ani Dec 17 '10 at 12:04
  • And your criticism is gratefully accepted -- I'm actually looking through the IL for the related code now :D You were right to call it out, thanks. – Tim Barrass Dec 17 '10 at 12:09
  • 1
    Be sure to edit in any interesting findings; votes aren't permanent. :) – Ani Dec 17 '10 at 12:14
  • 1
    +1 Nice work, Tim. But I don't understand the point about early escaping since the format-string has to be parsed (the `{0}` has to be substituted *eventually*). Can you clarify? Also, since you aren't really investigating the OP-codes, you might as well post the decompiled C# instead of the IL. :) – Ani Dec 18 '10 at 02:34
  • On IL -- exacly right, reflector would have been fine for the answer as-is. In fact, while looking through the IL I figured I had more questions about how much of it would actually be represented after jitting; a question of my own there, once I can phrase it well. In this case, I think it still answers the question without being a blog post. On early escaping -- I just wondered if there would be some way of escaping the need to parse an object to string if it were already a string, but ... So. An incomplete answer that's left me with as much to do as Darren. – Tim Barrass Dec 18 '10 at 20:52
2

If we neglect the output to console, this is essentially a choice between concatenation and format strings for a single concatenation.

It's a matter of preference.

Personally I would go with the + version in this case - it's shorter but equally readable. Format strings really shine when more complex formatting than this trivial concatenation is required, for example if you need arbitrary characters in the middle of the desired output. As SWeko points out, the format version lends itself to easier maintainability, but this is not a clincher as far as I'm concerned (it's not hard to go from one version to the other; even better, a tool like Resharper can do it on your behalf).

I would point out one (irrelevant for 90% of cases) fact: the + version is likely to be marginally more performant since there's no format-string to be parsed at run-time. (As with any other performance issue, you would need to measure for your specific cases to justify the choice of one over the other.)

Ani
  • 111,048
  • 26
  • 262
  • 307
2

In this case, first one is more readable. Perfromance-wise also probably a tiny quicker since there is only one parameter.

Using + concatenator becomes unreadable with a few parameters in the middle of the stentence.

Second one uses String.Format(...) which internally uses a StringBuilder.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
2

There are some differences, though.

The first line creates a new string then sends it to the WriteLine method.

In the second form, WriteLine calls internally string.Format() which uses internally a StringBuilder to build the final string that will be written.

In some cases might be better to use the second form (especially when there are more than one arguments).

Andrei Pana
  • 4,484
  • 1
  • 26
  • 27
1

Readability

From this perspective if I have a lot of parameters throughout interspersed with text, I'll use the latter approach. In the simple case you shown I'll opt for the former.

Performance

If using all strings they would be equivalent. If however, you're using a lot of parameters and you're concerned with performance I would suggest using the first style and a StringBuilder to create the line.

Winston
  • 500
  • 3
  • 3