Skip to content

Commit 76795fd

Browse files
pkulikovRon Petrusha
authored andcommitted
Updated string interpolation tutorial (#5200)
* 2 out of 6 complete commit * Sections 3 and 4 added * Added culture-related sections * Finalized tutorial * Fixed build warning * Link tutorial from the quickstart * Addressed nits * Addressed feedback on tutorial
1 parent 12bf6b5 commit 76795fd

File tree

5 files changed

+96
-103
lines changed

5 files changed

+96
-103
lines changed

docs/csharp/language-reference/tokens/interpolated.md

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ helpviewer_keywords:
1010
- "$ language element [C#]"
1111
- "string interpolation [C#]"
1212
- "interpolated string [C#]"
13-
author: "rpetrusha"
14-
ms.author: "ronpet"
13+
author: pkulikov
14+
ms.author: ronpet
1515
---
1616
# $ - string interpolation (C# Reference)
1717

18-
The `$` special character identifies a string literal as an *interpolated string*. An interpolated string looks like a template string that contains *interpolated expressions*. When the interpolated string is resolved to the result string, items with interpolated expressions are replaced by the string representations of the expression results. This feature is available in C# 6 and later versions.
18+
The `$` special character identifies a string literal as an *interpolated string*. An interpolated string is a string literal that might contain *interpolated expressions*. When the interpolated string is resolved to the result string, items with interpolated expressions are replaced by the string representations of the expression results. This feature is available in C# 6 and later versions of the language.
1919

2020
String interpolation provides a more readable and convenient syntax to create formatted strings than a [string composite formatting](../../../standard/base-types/composite-formatting.md) feature. The following example uses both features to produce the same output:
2121

2222
[!code-csharp-interactive[compare with composite formatting](../../../../samples/snippets/csharp/language-reference/tokens/string-interpolation.cs#1)]
2323

24-
> [!NOTE]
25-
> You cannot have any white space between the `$` and the `"` that starts the string. Doing so causes a compile-time error.
24+
## Structure of an interpolated string
25+
26+
To identify a string literal as an interpolated string, prepend it with the `$` symbol. You cannot have any white space between the `$` and the `"` that starts the string. Doing so causes a compile-time error.
2627

2728
The structure of an item with an interpolated expression is as follows:
2829

@@ -34,17 +35,19 @@ Elements in square brackets are optional. The following table describes each ele
3435

3536
|Element|Description|
3637
|-------------|-----------------|
37-
|`interpolatedExpression`|The expression to evaluate to get a result to be formatted. String representation of the `null` result is <xref:System.String.Empty?displayProperty=nameWithType>.|
38-
|`alignment`|The constant expression whose value defines the minimum number of characters in the string representation of the result of the interpolated expression. If positive, the string representation is right-aligned; if negative, it is left-aligned. For more information, see [Alignment Component](../../../standard/base-types/composite-formatting.md#alignment-component).|
39-
|`formatString`|A standard or custom format string that is supported by the type of the expression result. For more information, see [Format String Component](../../../standard/base-types/composite-formatting.md#format-string-component).|
38+
|`interpolatedExpression`|The expression that produces a result to be formatted. String representation of the `null` result is <xref:System.String.Empty?displayProperty=nameWithType>.|
39+
|`alignment`|The constant expression whose value defines the minimum number of characters in the string representation of the result of the interpolated expression. If positive, the string representation is right-aligned; if negative, it's left-aligned. For more information, see [Alignment Component](../../../standard/base-types/composite-formatting.md#alignment-component).|
40+
|`formatString`|A format string that is supported by the type of the expression result. For more information, see [Format String Component](../../../standard/base-types/composite-formatting.md#format-string-component).|
4041

4142
The following example uses optional formatting components described above:
4243

4344
[!code-csharp-interactive[specify alignment and format string](../../../../samples/snippets/csharp/language-reference/tokens/string-interpolation.cs#2)]
4445

45-
To include a brace ("{" or "}") in the text produced by an interpolated string, use two braces, "{{" or "}}". For more information, see [Escaping Braces](../../../standard/base-types/composite-formatting.md#escaping-braces).
46+
## Special characters
47+
48+
To include a brace, "{" or "}", in the text produced by an interpolated string, use two braces, "{{" or "}}". For more information, see [Escaping Braces](../../../standard/base-types/composite-formatting.md#escaping-braces).
4649

47-
As the colon (:) has special meaning in an interpolated expression item, in order to use a [conditional operator](../operators/conditional-operator.md) in an interpolated expression, enclose that expression in parentheses.
50+
As the colon (":") has special meaning in an interpolated expression item, in order to use a [conditional operator](../operators/conditional-operator.md) in an interpolated expression, enclose that expression in parentheses.
4851

4952
The following example shows how to include a brace into the result string and how to use a conditional operator in an interpolated expression:
5053

@@ -55,30 +58,36 @@ Verbatim interpolated strings use the `$` character followed by the `@` characte
5558
> [!NOTE]
5659
> The `$` token must appear before the `@` token in a verbatim interpolated string.
5760
61+
## Implicit conversions and specifying `IFormatProvider` implementation
62+
5863
There are three implicit conversions from an interpolated string:
5964

6065
1. Conversion of an interpolated string to a <xref:System.String> instance that is the result of interpolated string resolution with interpolated expression items being replaced with the properly formatted string representations of their results. This conversion uses the current culture.
6166

6267
1. Conversion of an interpolated string to a <xref:System.FormattableString> instance that represents a composite format string along with the expression results to be formatted. That allows you to create multiple result strings with culture-specific content from a single <xref:System.FormattableString> instance. To do that call one of the following methods:
6368

6469
- A <xref:System.FormattableString.ToString> overload that produces a result string for the <xref:System.Globalization.CultureInfo.CurrentCulture>.
65-
- A <xref:System.FormattableString.Invariant%2A> method that produces a result string for the <xref:System.Globalization.CultureInfo.InvariantCulture>.
70+
- An <xref:System.FormattableString.Invariant%2A> method that produces a result string for the <xref:System.Globalization.CultureInfo.InvariantCulture>.
6671
- A <xref:System.FormattableString.ToString(System.IFormatProvider)> method that produces a result string for a specified culture.
6772

73+
You also can use the <xref:System.FormattableString.ToString(System.IFormatProvider)> method to provide a user-defined implementation of the <xref:System.IFormatProvider> interface that supports custom formatting. For more information, see [Custom Formatting with ICustomFormatter](../../../standard/base-types/formatting-types.md#custom-formatting-with-icustomformatter).
74+
6875
1. Conversion of an interpolated string to an <xref:System.IFormattable> instance that also allows you to create multiple result strings with culture-specific content from a single <xref:System.IFormattable> instance.
6976

7077
The following example uses implicit conversion to <xref:System.FormattableString> to create culture-specific result strings:
7178

7279
[!code-csharp-interactive[create culture-specific result strings](../../../../samples/snippets/csharp/language-reference/tokens/string-interpolation.cs#4)]
7380

74-
If you are new to the string interpolation, check the [String interpolation in C#](../../quick-starts/interpolated-strings.yml) quickstart. For more examples, see the [string interpolation tutorial](../../tutorials/string-interpolation.md).
81+
## Additional resources
82+
83+
If you are new to the string interpolation, see the [String interpolation in C#](../../quick-starts/interpolated-strings.yml) quickstart. For more examples, see the [String interpolation in C#](../../tutorials/string-interpolation.md) tutorial.
7584

7685
## See also
7786
<xref:System.String.Format%2A?displayProperty=nameWithType>
7887
<xref:System.FormattableString?displayProperty=nameWithType>
7988
<xref:System.IFormattable?displayProperty=nameWithType>
8089
[Composite formatting](../../../standard/base-types/composite-formatting.md)
81-
[Strings](../../../csharp/programming-guide/strings/index.md)
82-
[C# Special Characters](../../../csharp/language-reference/tokens/index.md)
83-
[C# Programming Guide](../../../csharp/programming-guide/index.md)
84-
[C# Reference](../../../csharp/language-reference/index.md)
90+
[Strings](../../programming-guide/strings/index.md)
91+
[C# Programming Guide](../../programming-guide/index.md)
92+
[C# Special Characters](index.md)
93+
[C# Reference](../index.md)

docs/csharp/quick-starts/interpolated-strings-local.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,4 @@ You've completed the string interpolation quickstart.
164164

165165
You can continue with the [List collection](arrays-and-collections.md) quickstart in your own development environment.
166166

167-
Learn more about string interpolation in the [String interpolation](../language-reference/tokens/interpolated.md) topic in the C# Reference.
167+
For more information, see the [String interpolation](../language-reference/tokens/interpolated.md) topic and the [String interpolation in C#](../tutorials/string-interpolation.md) tutorial.

docs/csharp/quick-starts/interpolated-strings.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,5 @@ items:
133133
134134
You can continue these quickstarts in your own local development environment. Learn the basics of [local development](local-environment.md) and then pick a quickstart. You can try this same quickstart, move directly to the next quickstart, or start again with the [Numbers in C#](numbers-in-csharp-local.md) quickstart.
135135
136-
Learn more about string interpolation in the [String interpolation](../language-reference/tokens/interpolated.md) topic in the C# Reference.
136+
For more information, see the [String interpolation](../language-reference/tokens/interpolated.md) topic and the [String interpolation in C#](../tutorials/string-interpolation.md) tutorial.
137137

docs/csharp/tutorials/index.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ description: Explore these C# tutorials to learn how to build C# programs and le
44
ms.date: 09/07/2017
55
ms.assetid: fcc83b5b-fb68-4e48-9132-0882677d8056
66
---
7-
87
# C# Tutorials
98

109
The following tutorials enable you to build C# programs using [.NET Core](../../core/index.md):
1110

12-
* [Console Application](console-teleprompter.md): demonstrates Console I/O, the structure of a Console application, and
13-
the basics of the Task based asynchronous programming model.
14-
* [REST Client](console-webapiclient.md): demonstrates web communications, JSON serialization, and Object Oriented
15-
features in the C# language.
11+
* [Console Application](console-teleprompter.md): demonstrates Console I/O, the structure of a Console application, and the basics of the task-based asynchronous programming model.
12+
13+
* [REST Client](console-webapiclient.md): demonstrates web communications, JSON serialization, and object-oriented features in the C# language.
1614

1715
* [Inheritance in C# and .NET](inheritance.md): demonstrates inheritance in C#, including the use of inheritance to define base classes, abstract base classes, and derived classes.
1816

1917
* [Working with LINQ](working-with-linq.md): demonstrates many of the features of LINQ and the language elements that support it.
2018

2119
* [Microservices hosted in Docker](microservices.md): demonstrates building an ASP.NET Core microservice and hosting it in Docker.
2220

23-
* [String Interpolation](string-interpolation.md): demonstrates many of the uses for the `$` string interpolation in C#.
21+
* [String Interpolation](string-interpolation.md): demonstrates how to use string interpolation to create formatted strings in C#.
2422

25-
* [Using Attributes](attributes.md): how to create and use attributes in C#.
23+
* [Using Attributes](attributes.md): demonstrates how to create and use attributes in C#.

0 commit comments

Comments
 (0)