Using variables inside strings

thwd picture thwd · Aug 29, 2011 · Viewed 166.9k times · Source

In PHP I can do the following:

$name = 'John';
$var = "Hello {$name}";    // => Hello John

Is there a similar language construct in C#?

I know there is String.Format(); but I want to know if it can be done without calling a function/method on the string.

Answer

Fenton picture Fenton · Jul 27, 2015

In C# 6 you can use string interpolation:

string name = "John";
string result = $"Hello {name}";

The syntax highlighting for this in Visual Studio makes it highly readable and all of the tokens are checked.