I read that the StringBuilder type has a limitation (16 characters by default), and when you add some text to it, a new instance with a higher limit is created outside it, and the data is copied to This. I tried using the following code:
StringBuilder test = new StringBuilder("ABCDEFGHIJKLMNOP",16);
test.Append("ABC");
And the CIL generated for this was:
.maxstack 3
.locals init (class [mscorlib]System.Text.StringBuilder V_0)
IL_0000: nop
IL_0001: ldstr "ABCDEFGHIJKLMNOP"
IL_0006: ldc.i4.s 16
IL_0008: newobj instance void [mscorlib]System.Text.StringBuilder::.ctor(string, int32)
IL_000d: stloc.0
IL_000e: ldloc.0
IL_000f: ldstr "ABC"
IL_0014: callvirt instance class [mscorlib]System.Text.StringBuilder [mscorlib]System.Text.StringBuilder::Append(string)
IL_0019: pop
IL_001a: ret
Setting a limit, say 32:
StringBuilder test = new StringBuilder("ABCDEFGHIJKLMNOP",32);
test.Append("ABC");
Created exactly the same IL code. What I expect is to create a new instance in the first case and change the value of the instance in the second case, which obviously did not happen, tell me why?
source
share