Best ways to format and build strings in C# for readability

There are several ways to concatenate and format strings in c#. Below I will list a few. I do not take performance into this consideration as these methods are very close when looking at benchmarks. Also Performance is often not an issue - I believe readability of code is often better than little bit extra performance.

The old fashioned way

int one = 1, two = 2, three = 3; 
var testString = one + " plus " + two + " is " + three;

I just thought I would have this, as an honorable mention. This method of concatenating a string is good in the sense that all developers will get it. This will be understood by programmers from all modern programming languages. But that is also where my appraisel of this method ends. Concatenating strings this way is great if you only need to concatenate two strings. But as soon as you have several values and substrings things start to get messy. This is where the other ways come in handy.


int one = 1, two = 2, three = 3; 
var testString2 = string.Format("{0} plus {1} is {2}", one, two, three);

String.Format was my favourite way to format strings until recently. It makes it easy to place values in the middle of a string and it is great for readability. Simply replace the values you want within the string with {0}, where 0 is the index of the paramter you want to use for replacement.

String interpolation

int one = 1, two = 2, three = 3;
var testString3 = $"{one} plus {two} is {three}";

String Interpolation came around in c# 6 and comes with Visual Studio 2015. This is one of the caveats about string intepolation (and all other new features) - not everyone has the newest version of Visual Studio. This means that everyone on your team need to be up to date or they will get build/compile errors.

Other than that, String interpolation brings readability to a whole new level. It's easy to read and it is very compressed. It is also easy to use - it starts with a $ and then you just add the values you want inserted into the string in curly braces. There is no need to play around with indexes like when using string.format. At compile time it is transformed into a string.format, so all in all, it is just sugar syntax for a string.format.

I am definitely going to use string interpolation more in the future!

If you feel that I missed something, please write it in the comments below!