You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/tokens/interpolated.md
+25-16Lines changed: 25 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,19 +10,20 @@ helpviewer_keywords:
10
10
- "$ language element [C#]"
11
11
- "string interpolation [C#]"
12
12
- "interpolated string [C#]"
13
-
author: "rpetrusha"
14
-
ms.author: "ronpet"
13
+
author: pkulikov
14
+
ms.author: ronpet
15
15
---
16
16
# $ - string interpolation (C# Reference)
17
17
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.
19
19
20
20
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:
21
21
22
22
[!code-csharp-interactive[compare with composite formatting](../../../../samples/snippets/csharp/language-reference/tokens/string-interpolation.cs#1)]
23
23
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.
26
27
27
28
The structure of an item with an interpolated expression is as follows:
28
29
@@ -34,17 +35,19 @@ Elements in square brackets are optional. The following table describes each ele
34
35
35
36
|Element|Description|
36
37
|-------------|-----------------|
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).|
40
41
41
42
The following example uses optional formatting components described above:
42
43
43
44
[!code-csharp-interactive[specify alignment and format string](../../../../samples/snippets/csharp/language-reference/tokens/string-interpolation.cs#2)]
44
45
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).
46
49
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.
48
51
49
52
The following example shows how to include a brace into the result string and how to use a conditional operator in an interpolated expression:
50
53
@@ -55,30 +58,36 @@ Verbatim interpolated strings use the `$` character followed by the `@` characte
55
58
> [!NOTE]
56
59
> The `$` token must appear before the `@` token in a verbatim interpolated string.
57
60
61
+
## Implicit conversions and specifying `IFormatProvider` implementation
62
+
58
63
There are three implicit conversions from an interpolated string:
59
64
60
65
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.
61
66
62
67
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:
63
68
64
69
- 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>.
66
71
- A <xref:System.FormattableString.ToString(System.IFormatProvider)> method that produces a result string for a specified culture.
67
72
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
+
68
75
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.
69
76
70
77
The following example uses implicit conversion to <xref:System.FormattableString> to create culture-specific result strings:
71
78
72
79
[!code-csharp-interactive[create culture-specific result strings](../../../../samples/snippets/csharp/language-reference/tokens/string-interpolation.cs#4)]
73
80
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.
Copy file name to clipboardExpand all lines: docs/csharp/quick-starts/interpolated-strings-local.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -164,4 +164,4 @@ You've completed the string interpolation quickstart.
164
164
165
165
You can continue with the [List collection](arrays-and-collections.md) quickstart in your own development environment.
166
166
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.
Copy file name to clipboardExpand all lines: docs/csharp/quick-starts/interpolated-strings.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -133,5 +133,5 @@ items:
133
133
134
134
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.
135
135
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.
Copy file name to clipboardExpand all lines: docs/csharp/tutorials/index.md
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,22 +4,20 @@ description: Explore these C# tutorials to learn how to build C# programs and le
4
4
ms.date: 09/07/2017
5
5
ms.assetid: fcc83b5b-fb68-4e48-9132-0882677d8056
6
6
---
7
-
8
7
# C# Tutorials
9
8
10
9
The following tutorials enable you to build C# programs using [.NET Core](../../core/index.md):
11
10
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.
16
14
17
15
*[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.
18
16
19
17
*[Working with LINQ](working-with-linq.md): demonstrates many of the features of LINQ and the language elements that support it.
20
18
21
19
*[Microservices hosted in Docker](microservices.md): demonstrates building an ASP.NET Core microservice and hosting it in Docker.
22
20
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#.
24
22
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