String.Format Shortcut

by Randall 6/24/2008 5:19:00 PM

Well, I feel like I have totally missed the boat on something.

I love using string.Format() in order to construct messages and other things that need to be written to the console, etc.  For example:

    string msg = string.Format("I can't believe it's {0} already!", DateTime.Now.Date.ToShortDateString());
  Console.WriteLine(msg);

Anyway.  I just learned that the line above can be written as:

    Console.WriteLine("I can't believe it's {0} already!", DateTime.Now.Date.ToShortDateString());

Seems as if the compiler can determine that you're formatting a string and will infer the string.Format() function.

Too bad it can't infer the following:

    string msg = "I can't believe it's {0} already!", DateTime.Now.Date.ToShortDateString();

Currently rated 1.0 by 1 people

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Related posts

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

2/6/2012 2:16:42 PM

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Name of author Randall Sexton
Currently a .Net developer for Bechtel Corporation in Oak Ridge, TN.

E-mail me Send mail

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Pages

    Recent comments

    Don't show

    Authors

    Categories


    Logo Credit

    My logo was taken from CodingHorror.
    Jeff Atwood © Copyright 2007

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2012

    Sign in