Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 202
Andrew Avatar asked Aug 02 '12 08:08

Andrew


People also ask

What is line continuation character in C?

The backslash character ('') is the line continuation character. It must be the last character on a line. The preprocessor joins lines ending with a line continuation character into a single line (for purposes of preprocessing and compilation).

Which character is used as the line continuation character?

The line continuation character in IPL and JavaScript is the backslash (\). You use this character to indicate that the code on a subsequent line is a continuation of the current statement. The line continuation character helps you format your policies so that they are easier to read and maintain.

How do you move the code to the next line?

Use Shift+Enter to start a new line after the current line Use the key combination Shift+Enter to start a new line below the current line.


1 Answers

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.

like image 192
Steve Avatar answered Oct 16 '22 22:10

Steve