In vb.net the methods have their parameters using ByVal by default, it's better practice / common practice to make it explicit?
For example:
With ByVal:
Private Sub MySub(ByVal Q As Strin开发者_C百科g)
{
' ...
}
End Sub
Without ByVal:
Private Sub MySub(Q As String)
{
' ...
}
End Sub
According to Microsoft:
It is good programming practice to include either the ByVal or ByRef keyword with every declared parameter.
And if you use Visual Studio, it defaults to inserting ByVal
if you don't explicitly specify it.
Starting with VS 2010 SP1, ByVal
is no longer automatically inserted by the IDE.
I personally think it's better not to insert ByVal
manually, because:
- it's the default passing mechanism anyway, if neither
ByVal
norByRef
are explicitly specified. - omitting
ByVal
from method signature makesByRef
stand out. - it adds 'noise' to the code. VB.Net is already very verbose, no need to clutter the code with unnecessary
ByVal
s.
It is common practice that a method arguments can be specified at ByValue or ByReference. In VB.NET, the default argument type is ByVal
. In many programming language, method arguments are by-value
by default. If argument is not qualified with ByVal
or ByRef
then the argument type will be ByVal.
精彩评论