views:

182

answers:

6

Noticed this today when a patch was submitted with the following line:

lblCompletionTime.Text = String.Concat(trainingSkill.EndTime.ToLocalTime())

I can understand why the contributor used that syntax as above line concatenated two strings to form a multi-part date/time string.

Is there some hidden reason for having a single parameter overload of String.Concat() or was it included for "completeness" by the language designers.

I have replaced the line with:

lblCompletionTime.Text = trainingSkill.EndTime.ToLocalTime().ToString(CultureInfo.CurrentCulture)

Which has the same output.

+3  A: 

According to MSDN, String.Concat(object)

Creates the String representation of a specified object.

Charlie Salts
Object.ToString() says more or less the same thing. "Returns a String that represents the current Object.". I had realised what it did, it was more the why that I was interested in.
Richard Slater
+10  A: 

String.Concat(Object) gives you String.Empty when you pass null; ToString would crash with a null pointer exception.

Martin v. Löwis
What's funny is that they could make ToString an extension method on Object so that even calling myObject.ToString() when myObject is null would work.
Pierre-Alain Vigeant
@Pierre: "can" is rarely a good reason without a supporting "should"
STW
@Yoooder I agree with you :D
Pierre-Alain Vigeant
+4  A: 

I agree that it doesn't make much sense especially considering the fact that this is the implementation:

public static string Concat(object arg0)
{
        if (arg0 == null)
        {
                return Empty;
        }
        return arg0.ToString();
}

But it does validate that the argument isn't null so I guess that is something. Still you aren't concatenating anything so I think that this method could have been called

String.ToStringSafe(Object obj) { }

or something similar to better express what the function actually does.

Andrew Hare
A: 

Using String.Concat(Object) to convert an object to a string is the behavior that overload of the function was designed for.

String.Concat Method (Object)
Creates the String representation of a specified object.

Sebastian P.
+2  A: 

By looking at the documentation of String.Concat(object), the only advantage I see is that it accepts a null and returns an empty string, which can be an advantage when compared with object.ToString() which would throw a NullReferenceException. In your example it doesn't help because if trainingSkill.EndTime is null the program will break when invoking ToLocalTime. I preffer your correction, but I think there's no need for passing CultureInfo.CurrentCulture as a parameter since it is the default behaviour of DateTime.ToString()

Paulo Manuel Santos
The default behaviour seems a litle inconsitant particuarly on certain european installs .ToString will default to en-US, in fact FxCop will recommend replacing all instances of int.ToString(), DateTime.ToString() with the equavalant Overload that accepts an IFormatProvider.
Richard Slater
According to documentation both DateTime.ToString() and CurrentInfo.CurrentCulture use Thread.CurrentThread.CurrentCulture. So using DateTime.ToString(CurrentInfo.CurrentCulture) should always equal DateTime.ToString().
Paulo Manuel Santos
+1  A: 

In C# it may have little value (aside from the automatic empty string for a null value).

In .Net in general, I could this method signature being very useful in a functional language.

Austin Salonen
That is an interesting slant on the thought, I must admit I havn't looked at .NET based functional programming. What would stop you from using .ToString() in say F#?
Richard Slater
For a single object, ToString would make sense; but if it's processing a list recursively (Head | Tail -- probably not F# syntax but you get the idea), it will call the same function on both -- one being a single object, the other a list, or null.
Austin Salonen
FWIW, this is just a gut instinct. I have no proof that it actually works this way.
Austin Salonen