我见过几个程序员使用&
and +
用于字符串操作。
例如:
dim firstvar as string
dim secondvar as string
dim thirdvar as string
thirdvar = firstvar & secondvar
或者是:
thirdvar = firstvar + secondvar
有关系吗?如果是这样,为什么?
The + http://msdn.microsoft.com/en-us/library/9c5t70w2.aspx and 运营商是not在 VB.NET 中是相同的。
使用&
运算符表示您想要连接字符串,而+
运算符表示您要添加数字的意图。使用&
运算符会将运算两边都转换为字符串。当您有混合类型时(表达式的一侧是字符串,另一侧是数字),您对运算符的使用将决定结果。
1 + "2" = 3 'This will cause a compiler error if Option Strict is on'
1 & "2" = "12"
1 & 2 = "12"
"text" + 2 'Throws an InvalidCastException since "text" cannot be converted to a Double'
所以,我的指导方针(除了避免像这样的混合类型)是使用&
连接字符串时,只是为了确保编译器清楚您的意图,并避免涉及使用+
连接运算符。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)