Skip to content
Merged
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
14 changes: 14 additions & 0 deletions docs/csharp/language-reference/tokens/interpolated.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ The following example uses implicit conversion to <xref:System.FormattableString

:::code language="csharp" source="./snippets/string-interpolation.cs" id="Snippet4":::

## Combining multiple interpolated strings

To combine multiple interpolated strings, use the interpolation expression for each:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd change this to:

To combine multiple interpolated strings, add the `$` special character to each string literal.


```csharp
var firstWord = "Hello";
var secondWord = "World";

var combinedInterpolatedString = $"{firstWord}, " + $"{secondWord}!";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't this just be:

var combinedInterpolatedString = $"{firstWord}, {secondWord}!";

@BillWagner

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work, but wouldn't show the example as concatenating two interpolated strings. Admittedly, in this case, a single interpolated string would be clear. This would normally be done when all the source expressions were more complex.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gewarren it could be, for sure. I was using an example provided in the initial issue, which discussed concatenating multiple strings together specifically. So it may be that in the context of the example, the usefulness is lost.

Perhaps I could add something like "bear in mind, you could also accomplish this via" and use the syntax you've provided above?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SeanKilleen I'd prefer a more involved example where two different interpolated strings are constructed, then concatenated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Bill here. I don't think this is a good example.


Console.WriteLine(combinedInterpolatedString)
// output: "Hello, World!"
```

## Other resources

If you're new to string interpolation, see the [String interpolation in C#](../../tutorials/exploration/interpolated-strings.yml) interactive tutorial. You can also check another [String interpolation in C#](../../tutorials/string-interpolation.md) tutorial. That tutorial demonstrates how to use interpolated strings to produce formatted strings.
Expand Down