vb.net _下划线作为VB.net中变量前缀的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8514892/
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
Meaning of _ underscore as a variable prefix in VB.net
提问by StealthRT
What is the meaning of underscore in visual basic? I have this code:
Visual Basic中下划线是什么意思?我有这个代码:
Private _isAuthenticated As Boolean
Is that the same as doing this?
这和做这件事一样吗?
Private isAuthenticated As Boolean
Or does adding the underscore to the front of the name do something special?
或者在名称前面添加下划线有什么特别的作用吗?
回答by codechurn
FYI: if you are looking at VB code prior to the .NET era (ie: VB6, of which there is a ton out there) the _ character did have special meaning in that it was a line continuation character. Variables or lines could not begin with an _
仅供参考:如果您正在查看 .NET 时代之前的 VB 代码(即:VB6,其中有很多),_ 字符确实具有特殊含义,因为它是一个行继续符。变量或行不能以 _ 开头
Example of VB6 use of _:
VB6 使用 _ 的示例:
Dim str As String
str = "This is part one of a very long string" & _
"Notice that this is more text" & _
"AND SOME MORE"
I am pretty sure that in VB.NET the _ continues to function as a line continuation character, however the variable name restriction has obviously been lifted.
我很确定在 VB.NET 中 _ 继续用作行继续符,但是变量名称限制显然已取消。
回答by u710480
It's a convention. A leading _ often indicates that the variable is private to the class. This convention is commonly used in many different languages, not just VB.
这是一个约定。前导 _ 通常表示该变量是该类的私有变量。这种约定通常用于许多不同的语言,而不仅仅是 VB。
In a similar sense, it also indicates that the variable is the local variable behind a property.
同理,它也表示该变量是属性背后的局部变量。
However it has no significant meaning to the compiler.
然而,它对编译器没有重要意义。
回答by Phil Klein
Many use the underscore prefix for field members of the class. These variables should be scoped as Private
. This is just a convention however.
许多使用下划线前缀作为类的字段成员。这些变量的范围应为Private
. 然而,这只是一个约定。
回答by William
At the end of a line, it can be used to split code across multiple lines if it's preceded by a space & the very next character is the new line (_ is the last symbol on the line & followed by a space.
在一行的末尾,如果它前面有一个空格并且下一个字符是新行(_ 是该行的最后一个符号,后面跟一个空格),则它可用于将代码拆分为多行。
回答by Akshay
The _ (Underscore symbol) is used just because to notify that it is a Private variable.
_(下划线符号)仅用于通知它是一个私有变量。