Update
Please see this question: Does StringBuilder use more memory than String concatenation?. I believe Dan Taos' answers my question. I wanted to make sure that his answer is still sound as it is dated 2010? I believe this also answers my question: https://coders-corner.net/2014/08/20/concatenate-strings-in-c-operator-vs-string-concat-vs-stringbuilder/
Original Question
This approach is frowned upon using C#:
public string getMessage()
{
string message = "Hello";
string message += " my";
string message += " name";
string message += " is";
string message += " bert";
return message;
}
This is better:
public string getMessage()
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello");
sb.Append(" my");
sb.Append(" name");
sb.Append(" is");
sb.Append(" Bert");
return message.ToString();
}
How does this approach compare to the second approach:
public string getMessage()
{
return string.Concat("Hello", " my", " name", " is", " Bert");
}
I like the third approach because it is only one line of code. Are there any implications of using the third approach? I have looked at the .NET reference here: https://referencesource.microsoft.com/#mscorlib/system/string.cs,8281103e6f23cb5c and specifically the method with signature: public static String Concat(params String[] values). However, I still wanted to confirm?