First of all, what is literal constant?
Literal constant is the representation of a value of a string in the source code, example:
"isto é uma constante literal"
Another example:
var str = "foo";
In this example str is a string and "foo" one literal constant.
In our day-to-day life we don’t need to emphasize this difference, but when we talk about performance over strings it’s critical to differentiate. Because literal constants are values that are directly present in the source code this gives the compiler the opportunity to optimize these operations (such as concatenating at compile time).
Operator +
"A temperatura em " + cidade + " é de " + temperatura;
Pro:
- If concatenated literal constants the concatenation is performed at compile time.
- Is considered the standard.
Against:
- Barely readable; difficult to edit.
string. Concat
string.Concat("A temperatura em ", cidade, " é de ", temperatura);
In terms of performance is identical to the operator +
(except in the case of literal constants).
string. Format
string.Format("A temperatura em {0} é de {1}", cidade, temperatura);
Pro:
- Preferred by many when formatting concatenation (more readable).
- Easy to use a string concatenated several times in the original string (just repeat the index).
Against:
- Little performatic.
- An execution (not compilation) error is generated if an invalid number of parameters to be concatenated is passed.
- In a string with many concatenations, it may not be very friendly to keep keeping the numbers of the concatenation indices.
Stringbuilder
new StringBuilder("A temperatura em ").Append(cidade).Append(" é de ").Append(temperatura);
Stringbuilder is a specific class for string construction. The big difference from the previous methods is that while the traditional type System.String is an immutable object, that is, every operation creates a new string, already the Stringbuilder is changeable, making it the most performative in scenarios involving many operations (read concatenations in loops).
It is important to note that the use in the above example is a case in which nay whether to use Stringbuilder.
Considerations
We as programmers like this kind of subject matter very much but the reality is that worrying about performance in trivial string concatenations is the call micro optimization (notice the bad connotation).
As a general rule, I particularly follow:
- I try to use simple concatenation, via operator
+
.
- If by chance the project I’m working on takes another way,
string.Concat
for example, I will use it. Consistency is something important.
- When a large number of concatenations of literal constants is done prejudge by the operator
+
. This concatenation will be done at compile time saving precious processing cycles.
- When the strings involved are large, multiple operations, loop operations, in short, when a heavy job is done on strings use Stringbuilder. Do not use it for trivial work.
Take a look at this link, has performance comparison between the ways of concatenating
string
.– MeuChapeu
In C# 6.0 they added a new form of concatenation, through the '$'. Example: string str = "world"; $"Hello {str}"; Output: Hello world
– Douglas