thoughts on coding

August 11, 2010

String FormatWith Extension

Filed under: Uncategorized — Frantisek @ 12:45 pm

There are plenty of cases when it’s necessary to display any data with the texts. Yes, you can use

string.Format(“My age is {0} year(s)”, age);

but I prefer more fluent interface so I wrote the following extension:

    1 public static string FormatWith(this string text, params object[] args)

    2 {

    3     if (text.IsNullOrEmpty())

    4     {

    5         return string.Empty;

    6     }

    7 

    8     return string.Format(text, args);

    9 }

and its usage is

“My age is {0} year(s)”.FormatWith(age)

Isn’t it more fluent and better to read?

The original article was published at http://catarsa.com/Blog/Kaduk-Frantisek/2010/8/Extensions_FormatWith

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Blog at WordPress.com.

%d bloggers like this: