Bug in String concatenation

I have the following code:

Function NormalStringModification(s As String) As String
  Dim rs As String = ""
  For i = 0 To s.Length - 4 Step 5
    rs &= s.Substring(0, i) & "X"
  Return rs
End Function

When I compile this, and decompile to VB again, I see:

The check on startindex and lenght is not needed as this is already done in the Substring routine of String. The nothing (or Null) check is just plain wrong. When a string is nothing or Null in VB and you concatenate it, it is seen as an empty ("") string.
But also here, the needed checks are already done in the String.Concat method.

So, both checks are not needed, and the code can be compiled to (as it is by VB):

Or, in IL:

Logged as bugs://E25493.

this is not broken, only an optimization, correct?

The null check is a breaking change.

1 Like