String.Format() made easy

          i have been browsing through the web as usual, and found some interesting articles regarding string.Format() . For a quick summary. string.format is similar to sprintf function from the world of C .you can put placeholders in the formatstring along with specifying the formatting of the data and then passes the data. it internally call the object.ToString method passing it the format string u just passed to it and prepares the final string for you .

placeholders are of the form {<argument index>[,<alignment>][:<formatString><zeros>]} alignment is specified as positive value for right justified and negative for left justified.

String.Format usually  performs better then concatenating different string together, due to the reason that during string concatenation ,intermediate strings are created and  also its more readable atleast to my. The only thing that ping me is in the case where there is a long list arguments so you will be counting each time to see what its format string is ,unless ur IDE aids you with some intellisense.

anways enjoy the two great links

String Formatting in C# by Steve Tibbett
.Net Format String 101    by Kathy Kam

~ by faraz on February 25, 2007.

4 Responses to “String.Format() made easy”

  1. Hi Faraz,

    The reason why String.Format() is better for appending strings other than readability is because String.Format internally uses StringBuilder Object for concating all the strings. The StringBuilder Object is Mutable whereas Simple String is Immutable.

  2. exactly , thats why the intermeidate strings are created during string concatenation.
    check these
    http://channel9.msdn.com/wiki/default.aspx/Channel9.PerfSystemString

    and
    http://channel9.msdn.com/wiki/default.aspx/Channel9.PerfSystemTextStringBuilder

  3. Hey,

    I recommend you definately take a look at Perf & Scale PAG if you have’nt before, also mentioned in one of the above articles.

    I read some portions of it long time back before 2.0, but the guide sure is wonderful study about App Perf. Specially Chapter-5,6 and 14 are wonderful.

    http://msdn2.microsoft.com/en-us/library/ms998530.aspx

  4. a very nice read indeed

Leave a Reply