Does calling a method on a value type result in boxing in .NET?

后端 未结 4 2022
渐次进展
渐次进展 2020-12-05 04:38

I was just participating in Stack Overflow question Is everything in .NET an object?.

And one poster (in comments of accepted answer) seemed to think that p

4条回答
  •  清歌不尽
    2020-12-05 05:36

    Here's the IL for your code:

    L_0001: ldc.i4.5      // get a 5 on the stack
    L_0002: stloc.0       // store into x
    L_0003: ldloca.s x    // get the address of x on the stack
    L_0005: call instance string [mscorlib]System.Int32::ToString()  // ToString
    L_000a: stloc.1       // store in s
    

    So the answer in this case is no.

提交回复
热议问题