-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add example of concatenated interpolated strings #29785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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: | ||
|
|
||
| ```csharp | ||
IEvangelist marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var firstWord = "Hello"; | ||
| var secondWord = "World"; | ||
|
|
||
| var combinedInterpolatedString = $"{firstWord}, " + $"{secondWord}!"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why wouldn't this just be:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
There was a problem hiding this comment.
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: