What is the difference between performance between different types of string concatenation?

5

I know 4 different types of concatenation of string in C #:

// string Interpolation
$"valor {variavel} R$";

// Verbating string
@"texto qualquer
  pula a linha e continua o texto";

// concatenar 2 strings
"texto" + "texto";

// string.format
String.Format(variavel, " R$");

// Verbating string com string Interpolation
$@"UPDATE {variavel}
   SET campo = {variavel2}";

Until then I know what each command is for, but I would like to know what impact it has on memory and performance.

Ex: if I am not mistaken in concatenation using "+"

 "texto1" + "texto2"

It would have 2 references in memory "texto 1" , "texto2" and when it concatenates it creates a third with result "texto1 texto2" .

How does it work in other cases? Or if what I explained was wrong (it was more to make it easier to understand my doubt).

Because I use Resharper and it always recommends the string interpolation and the verbating string and I would like to know how easy it is to read and how it disrupts performance and memory recycling.

An example of the code that Resharper suggests for me to use the verbating string

var texto = "texto";

Hesuggeststhatyoustay:

vartexto=@"texto"

Itsuggestsmovingresourcetostringorusingverbatingstring,andalsosuggestscreatingaconstantforthestringthatitpointsto.

How far does it make it easier and when does it get in the way?

    
asked by anonymous 28.11.2018 / 21:49

1 answer

5

Congratulations, it's above average:)

Only one of them is explicit concatenation, and none is bound to concatenate, any optimization could eliminate this, if possible in the case.

If these are just artificial examples, it screwed up, because they might not happen as expected, or as in other scenarios, but you can speak more generally.

Swear that the Resharper suggests it this way? It does not make sense to me, in the way you posted it.

$"valor {variavel} R$";

This will be transformed to a string.Format() :) I wanted a simpler solution, at least for most cases, but it's like this.

@"texto qualquer
pula a linha e continua o texto";

/ p>

"texto" + "texto";

This will probably be optimized and will turn a single string into memory. If the compiler can not optimize a slightly different case it will be transformed into string.Concat() ", internally there may be a simple concatenation, or you can use a StringBuilder , or at least an optimization of it.

String.Format(variavel, " R$");

I think the syntax is wrong, but that's beside the point.

View source code (It has to go following the links on it). It does something close to a StringBuilder by assembling the string . There have been optimizations to reduce memory allocations (actually in the framework everything can be allocated much less than before, if you know what you are doing, and indeed allocation is a bad thing) and I think will optimize more. If all goes well there will be no excess allocations, only one will resolve.

$@"UPDATE {variavel}
  SET campo = {variavel2}";

It's the same already mentioned, there will be only one string formatting, which will require a cost to enter the values.

    
28.11.2018 / 22:13