如何在 C# 中使用带有下划线 (_) 的换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11773623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use line continuation character, with an underscore (_) in C#
提问by Andrew
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.
对不起,如果这个问题之前已经被问过,但是在搜索之后没有具体的问题出现。出于某种原因,该代码不允许我在 VS 中进行编译。
Cheers Andrew
干杯安德鲁
采纳答案by Steve
There is no Line Continuation Characterin 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 (";").
Line Continuation Character在 C# 中没有像在 VB.NET 中那样
在 C# 中存在;来分隔指令的结尾。
这意味着不需要行继续符,因为在到达分号 (";") 之前不会考虑行结束。
The +is the string concatenation operator and it does not means Line Continuation Character
该+是字符串连接运算符,它不来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.
有时,出于易读性或其他原因,您希望在不同的行上拆分单个字符串。
在这种情况下,您可以使用字符串连接运算符。
回答by Dervall
C# does not have line continuations. Just use a normal line break and the code should work just fine.
C# 没有续行。只需使用正常的换行符,代码应该可以正常工作。
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.
与 VB 相比,C#;用作语句终止符,它允许您随意格式化代码,而无需指示代码在下一行继续。
_in C# is a normal character, and could be used for variable names and such.
_在 C# 中是一个普通字符,可以用于变量名等。

