开发者

c# printing out string concatenations

I've often been puzzled whether there should be any prefere开发者_运维问答nce 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.


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.


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);


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).


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.


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.)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜