You can read all the C# performance tips from the following links,

Here, we will do the benchmarking for string interpolation, string format, string concat, and string builder.

  1. string str = "Akshay";
  2. string str1 = " Patel";
  3. string result = string.Empty;
  4. Stopwatch s1 = new Stopwatch();
  5. String Interpolation
  6. s1.Start();
  7. result = $ "{str} {str1} is an author.";
  8. Console.WriteLine("String Interpolation " + s1.ElapsedTicks.ToString());
  9. String.Format
  10. s1.Restart();
  11. result = string.Format("{0},{1} is an author.", str, str1);
  12. Console.WriteLine("String.Format " + s1.ElapsedTicks.ToString());
  13. String.Concat
  14. s1.Restart();
  15. result = string.Concat(str, str1, " is an auther");
  16. Console.WriteLine("String.Concat " + s1.ElapsedTicks.ToString());
  17. StringBuilder
  18. s1.Restart();
  19. StringBuilder sb = new StringBuilder();
  20. sb.Append(str);
  21. sb.Append(str1);
  22. sb.Append(" is an auther");
  23. result = sb.ToString();
  24. Console.WriteLine("String Builder " + s1.ElapsedTicks.ToString());
  25. Console.ReadLine();

We have adopted four different ways to add two string variables. Let’s see the benchmarking result.

C# Programming Performance Tips #3 - String Add

Benchmarking result suggests going with String.Concat.