Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
C# provides a feature called string interpolation, that lets you substitute the result of C# code into the middle of a string.
- Having to remember to put a space on either side of the concatenated variable is awkward:
Console.WriteLine("You entered " + entry + " cans");
- C# provides a feature called string interpolation, that lets you substitute the result of C# code into the middle of a string.
- Interpolation can make it easier to format strings like this one.
- To use interpolation in C#, type a dollar sign (
$
) right before the opening quote of a string. - Anywhere you want within the string, you can include an interpolation marker. A marker consists of an opening curly brace, and a closing curly brace.
- You can include any C# code you want between the curly braces.
- I'll do a math operation: `Console.WriteLine($"aaa {1 + 2} bbb");
- The code will be evaluated, the result will be converted to a string, and the resulting string will be substituted for the interpolation marker within the containing string:
aaa 3 bbb
- Next, I'll use the property
System.DateTime.Now
, which you can think of like a variable that's always set to the current date and time - Because we have a
using System;
directive at the top of this file, I can take off theSystem
namespace:Console.WriteLine($"aaa {DateTime.Now} bbb");
- That gives me the output:
aaa 5/21/19 4:00:34 PM bbb
- Lets us embed the values of variables:
string name = "Jay";
Console.WriteLine($"Hello {name}!");
- We can include multiple interpolation markers into a single string:
Console.WriteLine($"{DateTime.Now} {name}");
Let's try improving our cat food store code using string interpolation.
- We'll add a dollar sign at the start of the string.
- We'll remove the plus signs to combine all the concatenated strings into one.
- In their place, we'll add an interpolation marker that inserts the value of the
answer
variable:Console.WriteLine($"You entered {answer} cans");
- It's easy to remember to put spaces surrounding the interpolation marker because it would look funny if we didn't.
You can see in the output that it's inserting the value of the answer
variable into the string, and it's surrounded by spaces as it should be.
Having to remember to put a space on
either side of a concatenated variable
0:00
is awkward.
0:04
C# provides a feature
called string interpolation
0:04
that lets you substitute the result of
C# code into the middle of a string.
0:08
Interpolation can make it easier
to format strings like this one.
0:13
To use interpolation in C#,
0:17
you type a dollar sign right before
the opening code of a string.
0:18
Anywhere you want within the string,
you can include an interpolation marker.
0:22
A marker consists of an { and a }.
0:26
You can include any C# code you
want between the curly braces.
0:29
I'll do a math operation, 1 + 2.
0:35
Let's try taking this string and
passing it to Console.WriteLine.
0:38
Let me save that and try running it.
0:47
Whoops, I forgot a semicolon
at the end of the line.
0:54
One moment.
0:56
There we go,
let's try that again, dotnet run.
0:59
And there we are.
1:04
The string starts with aaa and a space.
1:04
And here's the results
of our math operation.
1:07
1 plus 2 is 3.
1:10
Then the string ends with a space and bbb.
1:11
Next, let me try using the property
System.DateTime.Now which you can think of
1:18
like a variable that's always set
to the current date and time.
1:23
Because we have a using System
directive at the top of this file,
1:27
I can take off the System name space.
1:30
Now let's save that and try running it.
1:33
And now,
instead of the result of a math operation,
1:38
we have the date interpolated
into the middle of our string.
1:40
We can use interpolation to embed
the values of variables into its stream.
1:46
So let's try setting that string variable,
and
1:50
then let's try interpolating that
variable into the middle of a string.
1:53
Save that, run it, and
there's the value of the main variable,
1:56
interpolated into the middle of a string.
2:02
If we want, we can include multiple
interpolation markers in a single string.
2:10
So for example, here's the string that's
going to contain both the current date and
2:14
the value of the name variable.
2:18
Let's save that and try running it.
2:20
And there it is, the date and
the value of names separated by a space.
2:23
Let's try improving our cat food store
code using string interpolation.
2:29
We'll had a dollar sign at
the start of the string and
2:33
we'll remove the plus signs to combine
all the concatenated strings into one.
2:36
In their place,
2:45
we'll add an interpolation marker that
inserts the value of the entry variable.
2:46
It’s easy to remember to put spaces
surrounding the interpolation marker
2:54
because it would look funny if we didn’t.
2:57
Let’s save this and try running it.
3:00
We’ll say we’re ordering 22 cans.
3:06
And you can see in the output that it's
inserting the value of the answer variable
3:09
into the string and
it's surrounded by spaces as it should be.
3:13
You need to sign up for Treehouse in order to download course files.
Sign up