String Concatenation and the Newline Constant

String variables can be connected together to build a larger String via the VB 2008 ampersand operator (&). As you may know, this technique is formally termed string concatenation:

Module Program Sub Main()

Console.WriteLine("***** Fun with Strings *****")

Dim s1 As String = "Programming the " Dim s2 As String = "PsychoDrill (PTP)" Dim s3 As String = s1 & s2 Console.WriteLine(s3) End Sub End Module

Note VB 2008 also allows you to concatenate String objects using the plus sign (+). However, given that the + symbol can be applied to numerous data types, there is a possibility that your String object cannot be "added" to one of the operands. Therefore use of the ampersand (&) is the recommend approach.

You may be interested to know that the VB 2008 & symbol is processed by the compiler to emit a call to the shared String.Concat() method. In fact, if you were to compile the previous code and open the assembly within ildasm.exe (see Chapter 1), you would find CIL code similar to what is shown in Figure 3-8.

j/ FunWithStrings.Program;:Main ; voidO


Find Next

Was this article helpful?

0 0

Post a comment