What does “& _” mean in VB?

2020-06-08 13:29发布

问题:

I'm copying some query statements from a legacy VB app to a C# app. I am not familiar with VB, although looking at it makes me want a VB (Victoria Bitter). I have come across queries constructed like this:

*SELECT dp_duckbill_accounts.platypus_no AS duckbill, t_accounts.name AS Name " & _ 
"FROM t_accounts INNER JOIN dp_duckbill_accounts ON  t_accounts.account_no = dp_duckbill_accounts.account_no " & _
"ORDER BY dp_duckbill_accounts.platypus_no*

The "& _" give me pause. If it was just "&" I would think it corresponds to "+" in C# to concatenate strings. But what in the world is the point of the underscore? Note the ampersand and the underscore are separated by a space.

回答1:

The underscore is the line continuation character. It allows the concatenation to include a different line. Like so:

x = "Hello " & "World"

x = "Hello " & _
    "World"

'this won't compile (pre vb.net 2010, anyway)
    x = "Hello " & 
    "World"

Line Continuation on MSDN

How to: Break and Combine Statements in Code (Visual Basic)



回答2:

_ means continue the statement on the following line.

so ... & _ means continue concatenating the string on the following line.

text = "One line string"
text = "Two line " & _
       "string"


回答3:

That is just a line continuation character that lets you continue to the next line.



回答4:

& - is used for string concatenation in same line. example - sConcatenatedString = "First" & "Second"

& _ - is used For string concatenation in different lines. example - sConcatenatedString = "First" &_ "Second"



标签: vb.net