Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3596322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:58:23+00:00 2026-05-18T19:58:23+00:00

I’ve often been puzzled whether there should be any preference over any of these

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:58:23+00:00Added an answer on May 18, 2026 at 7:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.