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

3/9/2010 10:50:37 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

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

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 2010

    Sign in