Difference between using the append method of the StringBulder class and the concatenation operator "+"

What is the difference in using the Append method of the StringBuilder and Concatenation classes with the "+" operator?

How does the Append method work more efficiently or faster than the + operator in concatenating two strings?

+4
source share
3 answers

First of all, Stringand StringBuilder- different classes.

Stringa class represents immutable types , but a class StringBuilderrepresents mutable types.

When you use +to combine your lines, it uses String.Concat. And each time it returns a new string object.

StringBuilder.Append . , .

The Sad Tragedy of Micro-Optimization Theater

. . . !

, , . , - -, , , .

, ? , , , . - 100 000 , 3,5 Core 2 Duo.

1: Simple Concatenation 606 ms
2: String.Format    665 ms
3: string.Concat    587 ms
4: String.Replace   979 ms
5: StringBuilder    588 ms
+5

, , , .

StringBuilder .

, , . , StringBuilder.

0

, . StringBuilder , .

, . , :

string s = "a" + "b";

, , , , StringBuilder, .

StringBuilder , , .

0
source

All Articles