String interpolation is another big word for impressing family members, with an actual very simple explanation. Also known as variable substitution, variable expansion, or variable interpolation, it is simply the process through which we can substitute certain placeholders in string literals with actual values from variables and expressions. If you still don't know what I'm talking about, remember how we used to insert variable values in the strings we printed at the console, in the previous lessons:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int age = 25;
int height = 158;
Console.WriteLine("My age is " + age + " and my height is " + height);
Console.ReadLine();
}
}
}
|
This works just fine, but it can become tedious and confusing to handle all those quotation marks. For this reason, C# specifies ways in which we can avoid them. The first one, which we already used in some previous examples, is called composite format string, and looks like this:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int age = 25;
int height = 158;
Console.WriteLine("My age is {0} and my height is {1}", age, height);
Console.ReadLine();
}
}
}
|
The wildcard values specified inside the curly brackets will be replaced with the values of the variables that follow after the text, and they are numbered starting from 0 in a consecutive and ascending order. For each wildcard parameter inside the string, a corresponding literal value or variable must be provided after the string. This seems better than the initial approach, it gets us rid of the countless quotation marks, but it still can create confusion if the number of wildcards don't match the number of parameters after the string, or if their order doesn't match. This is solved with string interpolation, which looks like this:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
int age = 25;
int height = 158;
Console.WriteLine($"My age is {age} and my height is {height}");
Console.ReadLine();
}
}
}
|
Notice the $ character in front of the text, this is the actual character that allows the interpolation to happen in the first place. Also remember that in order for the interpolation to work, the strings must be available at compile time as string literal. And, since interpolation is performed with curly brackets, if your text contains a legit curly bracket, you need to escape it, or you will receive an error. The escaping is done by two successive curly brackets, not by the backslash character:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
Console.WriteLine($"This is interpolation with }} characters included");
Console.ReadLine();
}
}
}
|
In this case, only one curly brackets will be printed at the console.
Speaking of escape sequences, C# also allows us to ignore anything in the string that would normally be interpreted as an escape sequence by placing a @ character in front of the string literal, like this:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
using
System;
namespace
HelloWorld
{
class
Program
{
static void Main(string[] args)
{
Console.WriteLine(@"C:\Program Files\");
Console.ReadLine();
}
}
}
|
In this case, we do not hawe to escape the literal string like this: C:\\Program Files\\, as we normally would. There is one exception to this, you do need an escape sequence for double quotation marks, and this is achieved by placing two double quotation marks in successive order: @"""". Be aware that the string still has the beginning and ending double quotation marks, so by escaping the character, only one quotation mark is actually displayed.