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

Advertisements

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

  3. a very nice read indeed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s