+ 和 & 的区别用于在 VB.NET 中连接字符串 [英] The difference between + and & for joining strings in VB.NET

查看:24
本文介绍了+ 和 & 的区别用于在 VB.NET 中连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

+& 在 VB.NET 中连接字符串有什么区别?

What is the difference between + and & for joining strings in VB.NET?

推荐答案

如果两个操作数都是字符串,则没有区别.然而,如果一个操作数是一个字符串,一个是一个数字,那么你就会遇到问题,请看下面的代码.

There's no difference if both operands are strings. However, if one operand is a string, and one is a number, then you run into problems, see the code below.

"abc" + "def" = "abcdef"
"abc" & "def" = "abcdef"
"111" + "222" = "111222"
"111" & "222" = "111222"
"111" & 222 = "111222"
"111" + 222 = 333
"abc" + 222 = conversion error

因此,我建议在要连接时始终使用 &,因为您可能试图将整数、浮点数、十进制数连接到字符串,这将导致异常,或者充其量,不要做你可能想让它做的事情.

Therefore I recommend to always use & when you mean to concatenate, because you might be trying to concatenate an integer, float, decimal to a string, which will cause an exception, or at best, not do what you probably want it to do.

这篇关于+ 和 & 的区别用于在 VB.NET 中连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆