Memory allocation in string concatenation in C# -


let,

string = “test”; string b = “test 2”; string c  = + b 

the output of c "testtest 2"

i know how memory allocated?

string = "test"; 

you create reference called a , pointing "test" object in memory.

string b = "test 2"; 

you create reference called b , pointing “test 2” object in memory.

string c  = + b; 

you allocating new memory address a + b (and process uses string.concat method.) because strings immutable in .net. , c reference assing new memory address.

here il code of this;

  il_0000:  nop   il_0001:  ldstr      "test"   il_0006:  stloc.0   il_0007:  ldstr      "test 2"   il_000c:  stloc.1   il_000d:  ldloc.0   il_000e:  ldloc.1   il_000f:  call       string [mscorlib]system.string::concat(string,                                                               string)   il_0014:  stloc.2   il_0015:  ldloc.2 

stloc.0 used, stores value on top of evaluation stack local memory slot 0.

ldstr instruction used load string memory or evaluation stack. necessary load values evaluation stack before can utilized.

the ldloc instruction load local instruction. ldloc places value of local variable on stack.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -