Tuesday, September 6, 2011

Dotnet format strings

Quite counter intuitively as far as I am concerned the following code snippets produce wildly varying results.

DateTime.Now.ToString("M");
DateTime.Now.ToString("M ");

Now, you would expect that the second line has the same output as the first but with a space appended. However date formats that are exactly one character long are treated differently.  There is a set of pre-baked format strings that come into play in that case, and another set that are used for strings 2+ characters.

I don't like it. I think it would have been better to use a char in the first call or a separate method name.

No comments:

Post a Comment