3

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.

0

6 Answers 6

10

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

Sign up to request clarification or add additional context in comments.

2 Comments

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: "can" is rarely a good reason without a supporting "should"
4

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.

Comments

3

According to MSDN, String.Concat(object)

Creates the String representation of a specified object.

1 Comment

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.
2

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()

2 Comments

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.
According to documentation both DateTime.ToString() and CurrentInfo.CurrentCulture use Thread.CurrentThread.CurrentCulture. So using DateTime.ToString(CurrentInfo.CurrentCulture) should always equal DateTime.ToString().
1

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.

3 Comments

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#?
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.
FWIW, this is just a gut instinct. I have no proof that it actually works this way.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.