In C#7, I'm trying to use a multiline interpolated string for use with FormttableString.Invariant but string concatenation appears to be invalid for FormttableString.
Per the documentation: A FormattableString instance may result from an interpolated string in C# or Visual Basic.
The following FormttableString multiline concatenation does not compile:
using static System.FormattableString;
string build = Invariant($"{this.x}"
+ $"{this.y}"
+ $"$this.z}");
Error CS1503 - Argument 1: cannot convert from 'string' to 'System.FormattableString'
Using an interpolated string without concatenation does compile:
using static System.FormattableString;
string build = Invariant($"{this.x}");
How do you implement multiline string concatenation with the FormattableString type?
(Please note that FormattableString was added in .Net Framework 4.6.)
The Invariant method is expecting the parameter of
FormattableStringtype. In your case, the parameter$"{this.x}" + $"{this.y}"becomes"string" + "string'which will evaluate tostringtype output. That's the reason you are getting the compile error asInvariantis expecting theFormattableStringand notstring.You should try this for single line text -
Output -
And to implement
multilineinterpolation, you can build the FormattableString like below and then use the Invarient.Output -