I have two situations:
static void CreateCopyOfString()
{
string s = "Hello";
ProcessString(s);
}
and
static void DoNotCreateCopyOfString()
{
ProcessString("Hello");
}
The IL for these two situations looks like this:
.method private hidebysig static void CreateCopyOfString() cil managed
{
// Code size 15 (0xf)
.maxstack 1
.locals init ([0] string s)
IL_0000: nop
IL_0001: ldstr "Hello"
IL_0006: stloc.0
IL_0007: ldloc.0
IL_0008: call void ConsoleApplication1.Program::ProcessString(string)
IL_000d: nop
IL_000e: ret
} // end of method Program::CreateCopyOfString
and
.method private hidebysig static void DoNotCreateCopyOfString() cil managed
{
// Code size 13 (0xd)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello"
IL_0006: call void ConsoleApplication1.Program::ProcessString(string)
IL_000b: nop
IL_000c: ret
} // end of method Program::DoNotCreateCopyOfString
In the first case, there is extra calls for string init
, stloc.0
and ldloc.0
.
Does this mean that the first case would perform weaker than the second case where the string is directly passed to the method instead of first storing it in the local variable?
I saw the question Does initialization of local variable with null impacts performance? but it seems to be little bit different than what I need to know here. Thanks.