I had a question the other day in an interview about the performance of StringBuilder and while I had run performance tests with StringBuilder in the past I had never really looked under the covers to determine why StringBuilder was or wasn't faster then string concatenation. So now I am going to ask you given the following code which function on the average is faster and why?

public static string StringBuilderConcat(string a, string b)
{
      
StringBuilder x = new StringBuilder();
       x.Append(a);
       x.Append(b);
       return x.ToString();
}

public static string StringConcat(string a, string b)
{
      
string x = a + b;
      
return x;
}

Now what if we introduce this function?

public static string StringBuilderConcat2(string a, string b)
{
       StringBuilder x =
new StringBuilder(a.Length + b.Length);
       x.Append(a);
       x.Append(b);
       return x.ToString();
}

--Eric (Grand Valley State University)