Andrew
Andrew

Reputation: 156

How to use line continuation character, with an underscore (_) in C#

ShippingConfirmationLabel.Text = _
string.Format("Using {0} shipping to:<br>", _
ShippingTypeRadioButtonList.SelectedValue);

This however works fine:

ShippingConfirmationLabel.Text = "Using " + ShippingTypeRadioButtonList.SelectedValue + "
shipping to:<br>";

Sorry if this question has been asked earlier however upon searching nothing concrete came up for this. For some reason that code doesn't let me compile in VS.

Cheers Andrew

Upvotes: 5

Views: 23058

Answers (2)

Steve
Steve

Reputation: 216333

There is no Line Continuation Character in C# like in VB.NET
In C# exist the ; to delimit the end of an instruction.
This means that there is no need for a line continuation character since a line is not considered over until you reach a semi colon (";").

The + is the string concatenation operator and it does not means Line Continuation Character

ShippingConfirmationLabel.Text = 
            "Using " + 
            ShippingTypeRadioButtonList.SelectedValue + 
            "shipping to:<br>"; 

As you can see, you could break the line at every point you like (of course not in the middle of a keyword or identifier) and close it with the ; terminator.

Sometimes, for legibility or other reasons, you want to break apart a single string on different lines.
In this case you could use the string concatenation operator.

Upvotes: 10

Dervall
Dervall

Reputation: 5744

C# does not have line continuations. Just use a normal line break and the code should work just fine.

C# in comparison to VB uses ; as a statement terminator, which allows you to format the code however you like without indicating that the code continues on the next line.

_ in C# is a normal character, and could be used for variable names and such.

Upvotes: 3

Related Questions