163

I've found that while string interpolation is really nice when applied to my existing code base's string Format calls, given the generally preferred column limit, the string rapidly becomes too long for a single line. Especially when the expressions being interpolated are complex. With a format string you have a list of variables that you can split into multiple lines.

var str = string.Format("some text {0} more text {1}", obj1.property, obj2.property); 

Does anyone have any preferred means of breaking up these lines?

I suppose you could do something like:

var str = $"some text { obj1.property }" + $" more text { obj2.property }; 
6
  • 1
    In my experience it's better to avoid interpolating complex expressions. Rather extract a variable in that case. If you do that, and you break up where you have newlines inside your strings, it will typically fit fine. Commented Aug 1, 2015 at 18:49
  • 2
    I'm confused by this question. I just want a multiline $"" that works like @"" Commented May 5, 2016 at 14:12
  • 3
    Colonel Panic, the question is asking about how to break up long interpolation lines so we don't violate column width requirements, without introducing linefeeds in the string literal itself. $@"" is great but any newlines introduced into that will be in the string literal. Commented May 5, 2016 at 14:24
  • 1
    To support \t\r\n in $@ see the question stackoverflow.com/questions/51991713/… Commented Aug 23, 2018 at 18:57
  • 2
    Check C# 11 Raw string literals at learn.microsoft.com/en-us/dotnet/csharp/programming-guide/…. Commented Feb 13, 2023 at 7:06

5 Answers 5

237

You can break the line into multiple lines, but I wouldn't say the syntax looks nice any more.

You need to use the $@ syntax to use an interpolated verbatim string, and you can place newlines inside the {...} parameters, like this:

string s = $@"This is all { 10 } going to be one long { DateTime.Now } line."; 

The string above will not contain any newlines and will actually have content like this:

This is all 10 going to be one long 01.08.2015 23.49.47 line.

(note, norwegian format)

Now, having said that, I would not stop using string.Format. In my opinion some of these string interpolation expressions looks really good, but more complex ones starts to become very hard to read. Considering that unless you use FormattableString, the code will be compiled into a call to String.Format anyway, I would say keep going with String.Format where it makes sense.

Sign up to request clarification or add additional context in comments.

6 Comments

Solid answer. I am pretty sure FormattableString boils down to a call to String.Format too.
This answer helped me figure out how to do the exact opposite of this. Thanks for the $@"" syntax!
@AlexBooker It's more complex than that. You can't do FormattableString.Invarian($"Hello {name}" + \n "are you the owner of {pet}?"); because it merges the two interpolated strings into a single simple string.
Newlines and tabs are respected in interpolated verbatim strings. Enter the following into the C# interactive window and the resulting string will be formatted:. > var name = "Simon"; var templateName = "How to interpolate a verbatim string"; var now = DateTime.UtcNow;. var output = $@"Hi {name}, This template is a demo of {templateName}. It was ran at {now.ToString("o")}"; > output. The output is "Hi Simon,\r\nThis template is a demo of How to interpolate a verbatim string.\r\n\r\nIt was ran at 2020-01-24T15:49:35.6678353Z"
@Enzoaeneas I think you overrated the formatting support in comments, but I understand and agree with what you're saying.
|
68

You can combine $ and @ together to get string interpolation and multi-line string literal:

var str = $@"some text { obj1.property } more text { obj2.property }"; 

But that will give you a NewLine character in between, so it might not be what you want.

4 Comments

But doesn't @ actually put the line feed into the literal?
It does. Updated my answer to explicitly say that.
Not only a new line but also the initial tab(s) of the new line. In order to correctly depict the actual string format when using $@" in visual studio you have to start each new line from the far left of the text editor.
With this solution you can't use e.g. \r and \n in the string
30

While OP asked for something else, I expect many people reading this question would like a multiline interpolated $"" that works like @"". To do that, use $@""

$@"Height: {height} Width: {width} Background: {background}" 

2 Comments

He doesn't want newlines in the output, just the code itself to wrap on different lines
He made it clear that this answer was intended for the audience who arrives searching how to do multiline strings using string interpolation syntax.
6

This is it:

var str = $"some text { obj1.property }" + $" more text { obj2.property }"; 

Note the second $ in the $"..." + $"..."

4 Comments

Note that this will make two calls to String.Format and also create three strings in memory (Side A, Side B, and A+B)
Wouldn’t this prevent you from using a FormattableString cast on the concatenated string?
In many implementations today, running on a GHz CPU with Gb's of memory, an extra call and an additional string will not give you performance problems. Performance problems will be caused by e.g. using an algorithm of O(n2). I think it's unfair to downvote this guy.
This no longer makes two calls to String.Format! github.com/dotnet/roslyn/issues/54584 godbolt.org/z/d86rGPz68
0

I have used StringBuilder within overridden ToString() as an example.

 // return employee data public override string ToString() { StringBuilder buffer = new StringBuilder(); buffer.AppendLine($"Number: {EmployeeNumber}"); buffer.AppendLine($"Name: {EmployeeName}"); buffer.AppendLine($"Address: {PostalAddress}"); buffer.AppendLine($"Phone: {PhoneNumber}"); buffer.AppendLine($"Age: {EmployeeAge}"); buffer.AppendLine($"Gender: {EmployeeGender}"); buffer.AppendLine($"Status: {EmployeeStatus}"); buffer.AppendLine($"Manager: {EmployeeManager}"); buffer.AppendLine($"Start: {EmployeeStartDate.ToShortDateString()}"); return buffer.ToString(); } 

2 Comments

then why not buffer.AppendFormat("{0}Number: {1}", Environment.NewLine, EmployeeNumber); ???
If this was done a lot it would not perform well. You are doing an interpolation per line and the adding it to the string builder. You would be much better off just doing multiple append calls and adding the new line character before the start of the next item. Your current way uses up memory for each string and interpolation result that needs to be garbage collected.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.