0

So yesterday I saw this tutorial on c# and I learned about string interpolation. I've always programmed in java and just used the + to add strings. Now I'm trying to find out why you would use interpolation over just adding these strings. For example:

int number = 203;
System.IO.Directory.CreateDirectory("C:/Temp/" + number);

I feel like this works perfectly and no reason why I should use interpolation. Interpolation for example:

int number;
System.IO.Directory.CreateDirectory("C:/Temp/{0}", number);

When I googled it, I found a performance test. In this test they found that interpolating is slower than adding both 2 variables to a string.

Could anyone please explain to me why or when I should use interpolation over adding, since interpolation performes worse?

Foitn
  • 604
  • 6
  • 25
  • 1
    I thinks it is just simplier. Especially when you have more variables. In c# 6.0 you can also use: `$"C:/Temp/{number}"`; – pwas Aug 29 '16 at 09:25
  • 2
    Your example of 'string interpolation' is totally wrong – Steve Aug 29 '16 at 09:26
  • 1
    Note that string interpolation in C# 6 (there is no string interpolation before that) internally uses `string.Format`, so you are essentially asking why one would use `string.Format` over concatenating manually. – poke Aug 29 '16 at 09:27
  • Alright, I get why it might be easier if you are used to it. But what about the performance, wouldn't it be worth to just go for the optimized method? – Foitn Aug 29 '16 at 09:34
  • [When is it better to use String.Format vs string concatenation?](http://stackoverflow.com/q/296978/216074) – poke Aug 29 '16 at 09:44
  • @poke Thank you, I hadn't seen the part about the performance. – Foitn Aug 29 '16 at 09:46

0 Answers0