As in VB 6, VB 2005 String types are used to represent text and are a good example of a reference type, as you saw in "Variables," earlier in this chapter. Strings in .NET are immutable, which means that once you've assigned a value to a string variable, it cannot be changed. If the value of a string variable is changed, another string object is created during runtime. Consider this example:
Dim st As String st = "Hello" st &= " World!" MsgBox(st) ' prints "Hello World!"
In the above example, two string objects are involved: one for the initialization and one for the concatenation. This problem gets worse if you are doing concatenation in a loop, like the following:
Dim i As Integer, str As String = "" For i = 0 To 10000 str &= i.ToString Next
A much more efficient way to manipulate strings is to use the StringBuilder class, located in the System.Text namespace:
Dim i As Integer, str As New _ System.Text.StringBuilder() For i = 0 To 10000 str.Append(i.ToString) Next