You want to reduce the time spent concatenating strings in an application that performs this operation repeatedly.
Concatenate strings with a
instead of the classic
+ concatenation operators.
Example 16-4 through Example 16-6 show the
.aspx file and
the VB and C# code-behind files for our application that demonstrates
the performance difference between using the classic string operators
StringBuilder object to perform
concatenation. Our example concatenates two strings repeatedly, and
then calculates the average time per concatenation for the two
approaches. The output of the application is shown in Figure 16-1.
Figure 16-1. Measuring string concatenation performance output
In the common language runtime (CLR), strings are immutable, which
means that once they have been created they cannot be changed. If you
concatenate the two strings,
str2, shown in the following code fragment, the
resulting value of
str1 = "12345" str2 = "67890" str1 = str1 & str2 str1 = "12345"; str2 = "67890"; str1 = str1 + str2;
The way in which this concatenation is ...