小编Hum*_*bie的帖子

Stringbuilder does not create new instance if the initial capacity set too small but a book says otherwise

StringBuilder sb = new StringBuilder("test", 4);
sb.Append('\n');
sb.AppendLine("test1");
sb.AppendLine("test2");
sb.AppendLine("test3");
sb.AppendLine("test4");
Run Code Online (Sandbox Code Playgroud)

looking on IL code there is only one newobj line, but I thought there should be more instances of StringBuilder class since it should increase its capacity by creating new object? Or I got it wrong?

// [3 1 - 3 49]
    IL_0000: ldstr        "test"
    IL_0005: ldc.i4.4
    IL_0006: newobj       instance void [System.Runtime]System.Text.StringBuilder::.ctor(string, int32)
    IL_000b: stloc.0      // sb

    // [4 1 - 4 17]
    IL_000c: ldloc.0      // sb
    IL_000d: ldc.i4.s …
Run Code Online (Sandbox Code Playgroud)

c# stringbuilder

0
推荐指数
1
解决办法
94
查看次数

标签 统计

c# ×1

stringbuilder ×1