Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/csharp/programming-guide/strings/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ Beginning with C# 11, you can combine *raw string literals* with string interpol

:::code language="csharp" source="./snippets/StringInterpolation.cs" id="InterpolationExample":::

### Verbatim String Interpolation

C# also allows verbatim string interpolation, for example across multiple lines, using the `$@` or `@$` syntax.

To interpret escape sequences literally, use a [verbatim](../../language-reference/tokens/verbatim.md) string literal. An interpolated verbatim string starts with the `$` character followed by the `@` character. Starting with C# 8.0, you can use the `$` and `@` tokens in any order: both `$@"..."` and `@$"..."` are valid interpolated verbatim strings.

:::code language="csharp" source="./snippets/VerbatimStringInterpolation.cs" id="VerbatimStringInterpolation":::

### Composite formatting

The <xref:System.String.Format%2A?displayProperty=nameWithType> utilizes placeholders in braces to create a format string. This example results in similar output to the string interpolation method used above.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace StringExamples;
public static class VerbatimInterpolation
{
public static void VerbatimInterpolationExample()
{
// <VerbatimStringInterpolation>
var jh = (firstName: "Jupiter", lastName: "Hammon", born: 1711, published: 1761);
Console.WriteLine($@"{jh.firstName} {jh.lastName}
was an African American poet born in {jh.born}.");
Console.WriteLine(@$"He was first published in {jh.published}
at the age of {jh.published - jh.born}.");

// Output:
// Jupiter Hammon
// was an African American poet born in 1711.
// He was first published in 1761
// at the age of 50.
// </VerbatimStringInterpolation>
}
}