Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# FormattableString concatenation for multiline interpolation

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.)

like image 867
Eugene Avatar asked Oct 12 '18 23:10

Eugene


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

The Invariant method is expecting the parameter of FormattableString type. In your case, the parameter $"{this.x}" + $"{this.y}" becomes "string" + "string' which will evaluate to string type output. That's the reason you are getting the compile error as Invariant is expecting the FormattableString and not string.

You should try this for single line text -

public string X { get; set; } = "This is X";
public string Y { get; set; } = "This is Y";
public string Z { get; set; } = "This is Z";
string build = Invariant($"{this.x} {this.y} {this.z}");

Output -

This is X This is Y This is Z

And to implement multiline interpolation, you can build the FormattableString like below and then use the Invarient.

FormattableString fs = $@"{this.X}
{this.Y}
{this.Z}";
string build = Invariant(fs);

Output -

This is X

This is Y

This is Z

like image 54
Vinit Avatar answered Oct 18 '22 03:10

Vinit