Skip to content

Commit e8f0bec

Browse files
committed
Review.
1 parent 7bcef31 commit e8f0bec

3 files changed

Lines changed: 11 additions & 14 deletions

File tree

docs/csharp/fundamentals/strings/interpolation.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ ai-usage: ai-assisted
1717

1818
:::code language="csharp" source="snippets/interpolation/Program.cs" id="general":::
1919

20-
Each `{ }` is an *interpolation expression*. C# evaluates the expression, converts the result to a string by calling its `ToString` method, and substitutes the text into the result. If the expression evaluates to `null`, C# substitutes an empty string. Interpolated strings are a more readable alternative to <xref:System.String.Format*?displayProperty=nameWithType> and support the full [composite formatting](../../../standard/base-types/composite-formatting.md) feature set.
20+
Each `{ }` is an *interpolation expression*. C# evaluates the expression, converts the result to a string by calling its `ToString()` method, and substitutes the text into the result. The string interpolation for a `null` expression is the empty string. In most cases, the default conversion produces the output you want, and you don't need to do anything more.
2121

22-
For the language-reference treatment of the syntax and the underlying handler types, see [interpolated string token](../../language-reference/tokens/interpolated.md). For performance-focused topics such as `Span<char>` interpolation and custom interpolated string handlers, see [String operations](../../language-reference/builtin-types/string-operations.md).
22+
Interpolated strings are a more readable alternative to <xref:System.String.Format*?displayProperty=nameWithType>, and they support the full [composite formatting](../../../standard/base-types/composite-formatting.md) feature set. Anything you can do with a classic positional format string—format specifiers, alignment, culture-aware formatting, and constant strings—you can also do with an interpolated string. The rest of this article covers those options. Reach for them only when you need finer control over the result; otherwise, the plain `$"{expression}"` form is enough.
23+
24+
For the language-reference treatment of the syntax and the underlying handler types, see [interpolated string](../../language-reference/tokens/interpolated.md) reference. For performance-focused topics such as `Span<char>` interpolation and custom interpolated string handlers, see [String operations](../../language-reference/builtin-types/string-operations.md).
2325

2426
## Apply a format string
2527

@@ -29,7 +31,7 @@ To control how an expression result is formatted, follow the expression with a c
2931
{<expression>:<formatString>}
3032
```
3133

32-
The following example formats a date and a numeric value:
34+
The following example formats a <xref:System.DateTime> and a <xref:System.Double> value:
3335

3436
:::code language="csharp" source="snippets/interpolation/Program.cs" id="FormatString":::
3537

@@ -63,22 +65,19 @@ For paths and other strings that contain backslashes, prefer an [interpolated ra
6365

6466
## Use a conditional expression
6567

66-
The colon has special meaning inside an interpolation expression, so wrap a [conditional operator](../../language-reference/operators/conditional-operator.md) in parentheses:
68+
The colon has special meaning inside an interpolation expression, so wrap a [conditional exoressuin](../../language-reference/operators/conditional-operator.md) in parentheses:
6769

6870
:::code language="csharp" source="snippets/interpolation/Program.cs" id="conditional":::
6971

7072
## Span an expression across multiple lines
7173

72-
For better readability, break long interpolation expressions across multiple lines. The following example uses an [interpolated raw string literal](../../language-reference/tokens/raw-string.md) so the expression body can wrap freely:
74+
For better readability, break long interpolation expressions across multiple lines. Multi-line interpolation expressions are available since C# 11. The following example adds newlines between the `{` and `}` characters to separate the interpolated expressions from the literal text:
7375

7476
:::code language="csharp" source="snippets/interpolation/Program.cs" id="newlines":::
7577

76-
> [!NOTE]
77-
> Multi-line interpolation expressions are available since C# 11.
78-
7978
## Build constant strings
8079

81-
When every interpolated expression is itself a constant `string`, the whole interpolated string is also a `const`. That makes it usable for attribute arguments, `switch` patterns, and other contexts that require compile-time constants:
80+
You can build constant interpolated strings when every interpolated expression is a constant value.That makes it usable for attribute arguments, `switch` patterns, and other contexts that require compile-time constants:
8281

8382
:::code language="csharp" source="snippets/interpolation/Program.cs" id="ConstantInterpolated":::
8483

docs/csharp/fundamentals/strings/snippets/interpolation/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,9 @@ private static void Newlines()
126126
{
127127
// <newlines>
128128
int[] numbers = [3, 1, 4, 1, 5, 9, 2, 6];
129-
Console.WriteLine($"""
130-
Total: {
129+
Console.WriteLine($"Total: {
131130
numbers.Sum()
132-
}, average: {numbers.Average():F2}.
133-
""");
131+
}, average: {numbers.Average():F2}.");
134132
// => Total: 31, average: 3.88.
135133
// </newlines>
136134
}

docs/csharp/language-reference/builtin-types/string-operations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ai-usage: ai-assisted
88

99
# String operations: pattern matching, performance, and span-based search
1010

11-
This article covers string operations that go beyond the everyday `Contains`, `IndexOf`, and `Split` methods covered in the Fundamentals [Search strings](../../fundamentals/strings/search.md) and [Split strings into substrings](../../fundamentals/strings/split.md) articles. It focuses on three areas: regular-expression pattern matching, allocation-free search over <xref:System.ReadOnlySpan%601>, and performance considerations for `string` comparison.
11+
This article covers three string operations: regular-expression pattern matching with <xref:System.Text.RegularExpressions.Regex?displayProperty=nameWithType>, allocation-free search over <xref:System.ReadOnlySpan%601>, and choosing a <xref:System.StringComparison> value for correct, fast comparisons.
1212

1313
## Find specific text by using regular expressions
1414

0 commit comments

Comments
 (0)