Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# printing out string concatenations

Tags:

string

c#

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.

like image 310
Darren Young Avatar asked Dec 07 '22 00:12

Darren Young


1 Answers

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.

like image 179
Tim Barrass Avatar answered Jan 29 '23 16:01

Tim Barrass