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

I know of 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 of the commands is for, the more I would like to know what impacts they have on memory and performance.

Ex: if I'm not mistaken concatenation using the " + "

 "texto1" + "texto2"

It would have 2 References In Memory "texto 1", "texto2" and when it concatenates it creates a third with the result "texto1 texto2".

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

Because I use Resharper and it always recommends string interpolation and verbating string and I would like to know how far it makes reading easier and how far it hinders performance and memory recycling.

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

var texto = "texto";

IDE showing or Resharper indicating

He suggests that stay:

var texto = @"texto"

IDE showing or Resharper indicating

He suggests moving to the resource to the string or using the verbating string, and also suggests creating a constant for the string he points to.

IDE showing or Resharper indicating

How Far Does It facilitate and when does it hinder?

Author: Maniero, 2018-11-28

1 answers

Congratulations, it's above average:)

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

If these are just artificial examples, screwed up, because they may not happen as you expect, or as in other scenarios, but you can speak in a more generic way.

Swear Resharper suggests it that way? It doesn't make sense to me, in the way posted.

$"valor {variavel} R$";

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

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

Turns one thing and is the same as having no more than one line, except that a skip-the-line character will be included within the string, but for all intents and purposes it's one thing.

"texto" + "texto";

This will probably be optimized and will turn into a single string in memory. If the compiler fails to optimize a slightly different case it will be transformed into string.Concat(), there internally it can 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 doesn't come to the case.

See the source code (you have to follow the links in it). It does something close to a StringBuilder by mounting the string. Have there have been optimizations to reduce memory allocations (actually in the framework all today you can allocate far less than before, if you know what you are doing, and in fact allocation is something bad) and I think they will optimize more. If all goes well there will be no excess allocations, only one will solve.

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

Is the same as already mentioned, there will be a single string doing a formatting, which will require a cost to insert the values.

 5
Author: Maniero, 2020-08-18 18:58:48