C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Cats: This is an integer local variable. It equals 100 (there are lots of cats). It is inserted into the "{cats}" part of the literal.
Dogs: This equals 2. It is inserted into the interpolated string literal at the "{dogs}" position.
C# program that uses string interpolation
using static System.Console;
class Program
{
static void Main()
{
int cats = 100;
int dogs = 2;
// Create a string with string interpolation syntax.
string animals = $"cats = {cats} and dogs = {dogs}";
// Call Console.WriteLine.
WriteLine(animals);
}
}
Output
cats = 100 and dogs = 2
Result: The second array element is inserted into the resulting string. We can use "result" like any other string.
C# program that uses array with string interpolation
using System;
class Program
{
static void Main()
{
int[] values = { 10, 20, 30 };
// Test string interpolation with array values.
string result = $"The second value is {values[1]}";
Console.WriteLine(result);
}
}
Output
The second value is 20
C# program that uses math
using System;
class Program
{
static void Main()
{
int id = 100;
// We can use an expression with a string interpolation.
string result = $"The multiplied ID is {id * 10}";
Console.WriteLine(result);
}
}
Output
The multiplied ID is 1000
C# program that uses method call
using System;
class Program
{
static int Paws(int cats)
{
// Four paws per cat.
return cats * 4;
}
static void Main()
{
// A string interpolation can call a method.
string result = $"The paw count is {Paws(5)}";
Console.WriteLine(result);
}
}
Output
The paw count is 20
C# program that shows character error
class Program
{
static void Main()
{
// We cannot use some characters in the same way.
string result = $"Size is }";
}
}
Output
Program.cs(6,35,6,36): error CS8086:
A '}' character must be escaped (by doubling) in an interpolated string.
Note: This is a downside to string interpolation syntax, but it is needed to have special delimiter characters.
C# program that uses brace character correctly
class Program
{
static void Main()
{
// Double the char.
string result = $"My favorite char is }}.";
System.Console.WriteLine(result);
}
}
Output
My favorite char is }.
Tip: Make sure to correctly close all the interpolated expressions if you wish to compile your program.
C# program that shows syntax errors
class Program
{
static void Main()
{
string result = $"Hello, {";
}
}
Output
error CS8076: Missing close delimiter '}' for interpolated expression started with '{'.
error CS1010: Newline in constant
error CS1002: ; expected
Version 1: This loop tests a string interpolation with two variable accesses. Its value is tested for correctness.
Version 2: This version uses a string concat expression. The plus signs are compiled to a string.Concat method call.
string.ConcatVersion 3: Here we use string.Format. We use substitution markers to insert the values of cats and dogs.
string.FormatResult: String.Concat was fastest. String interpolation (version 1) and string.Format were about the same speed.
C# program that benchmarks string interpolation
using System;
using System.Diagnostics;
class Program
{
const int _max = 1000000;
static void Main()
{
int cats = int.Parse("10");
int dogs = int.Parse("2");
// Version 1: use string interpolation.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
string result = $"{cats} cats and {dogs} dogs";
if (result[0] != '1')
{
return;
}
}
s1.Stop();
// Version 2: use string concat.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
string result = cats + " cats and " + dogs + " dogs";
if (result[0] != '1')
{
return;
}
}
s2.Stop();
// Version 3: use string format method.
var s3 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
string result = string.Format("{0} cats and {1} dogs", cats, dogs);
if (result[0] != '1')
{
return;
}
}
s3.Stop();
Console.WriteLine(s1.Elapsed.TotalMilliseconds);
Console.WriteLine(s2.Elapsed.TotalMilliseconds);
Console.WriteLine(s3.Elapsed.TotalMilliseconds);
}
}
Output
376.5875 ms, String interpolation
293.1515 ms, Concat (+)
369.2315 ms, String Format