Why is there an overload for String.Concat () that takes a single parameter in .NET.

This was noted today when the patch was sent with the following line:

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

I can understand why the contributor used this syntax, like the line above, concatenated two lines to form a multi-point date / time string.

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

I replaced the line:

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

which has the same result.

+3
source share
6 answers

String.Concat (Object) provides you with String.Empty when passing null; ToString will fail with a null pointer error.

+10

, , , :

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

, null, , -. -, ,

String.ToStringSafe(Object obj) { } 

- , , .

+4
+3

String.Concat(object), , , , null , . ToString() NullReferenceException. , trainingSkill.EndTime null, ToLocalTime. , , CultureInfo.CurrentCulture , DateTime.ToString()

+2

# ( ).

.Net , .

+1
source

Using String.Concat(Object)to convert an object to a string is the behavior for which function overloading was intended.

String.Concat method (object)
Creates a string representation of the specified object.

0
source

Source: https://habr.com/ru/post/1716352/


All Articles