Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

concatenation - Concat strings by & and + in 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?

Example:

    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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

& 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...