Concat字符串&和+在VB.Net [英] Concat strings by & and + in VB.Net

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

问题描述

&之间是否有任何区别?和+运算符同时连接字符串?
如果是,那么有什么区别?如果不是,那么为什么下面的代码会生成异常?

Is there any difference between & and + operators while concatenating string? if yes, then what is difference? And if No, then why below code generating exception?

示例:

    Dim s, s1, t As String
    Dim i As Integer

    s1 = "Hello"
    i = 1

    s = s1 & i
    t = s1 + i  //Exception here

    If s = t Then
        MessageBox.Show("Equal...")
    End If


推荐答案

&和+都是串联运算符,但是当您在使用+时指定整数时,vb.net会尝试将 Hello转换为整数以进行加法运算。如果将 Hello更改为 123,将得到结果124。

& and + are both concatenation operators but when you specify an integer while using +, vb.net tries to cast "Hello" into integer to do an addition. If you change "Hello" with "123", you will get the result 124.

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

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