Jeremiah Gowdy
Jeremiah Gowdy

Reputation: 5622

Long string interpolation lines in C#6

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 };

Upvotes: 163

Views: 58113

Answers (5)

Colonel Panic
Colonel Panic

Reputation: 137574

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}"

Upvotes: 30

Christopher Govender
Christopher Govender

Reputation: 555

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();
    }

Upvotes: 0

CallMeLaNN
CallMeLaNN

Reputation: 8568

This is it:

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

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

Upvotes: 5

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391336

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.

Upvotes: 237

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

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.

Upvotes: 68

Related Questions